reportingApp.controller('tradeController', function($scope) {
$scope.trades = [
//{ticker: 'AAPL', shares: 100, limit: 500},
//{ticker: 'GE', shares: -400, limit: 600},
];
$scope.addNewTrade = function () {
console.log ('addNewTade executes');
$scope.trades.push({ticker: "", shares: "", limit: ""});
$("table tr:last td:first").focus();
}
focus()函数不起作用,因为在它运行时,html行还没有改变。我应该把焦点代码放在哪里?
答案 0 :(得分:1)
尝试我的自定义指令:
app.directive('customAutofocus', function() {
return{
restrict: 'A',
link: function(scope, element, attrs){
scope.$watch(function(){
return scope.$eval(attrs.customAutofocus);
},function (newValue){
if (newValue == true){
element[0].focus();
}
});
}
};
})
使用它:
custom-autofocus="$index == trades.length-1"
当元素是最后一个时,将元素设置为焦点。