我有一个名为keepFocus的指令被调用,但$element.focus
部分没有创建焦点。
.directive("keepFocus", ['$timeout', function ($timeout) {
/*
Intended use:
<input keep-focus ng-model='someModel.value'></input>
*/
return {
restrict: 'A',
require: 'ngModel',
link: function ($scope, $element, attrs, ngModel) {
ngModel.$parsers.unshift(function (value) {
// $timeout(function () {
// $element[0].focus();
// },500);
//setTimeout(function() { $element[0].focus() }, 3000);
//this works
setTimeout(function() { $('.cart-item-quantity input')[0].focus() }, 3000);
return value;
});
}
};
}])
到目前为止我测试的内容:
$('.cart-item-quantity input') == $element //false
$('.cart-item-quantity input')[0] == $element[0] //false
最后的结果让我感到惊讶,因为元素看起来完全相同,直到我仔细观察并看到$element[0]
已经ng-valid ng-dirty
类,而jQuery版本有ng-pristine ng-valid
类
我以前从未使用过角度工作(正在研究别人的项目),所以不确定从哪里开始。
如果有人能指出我正确的方向,我们将非常感激。
提前致谢
更新
找到了罪魁祸首,只需要弄清楚如何绕过它!
$scope.update = function (skuid, quantity) {
quantity = sanitizeQuantity(quantity);
$http
.post(shoppingCartServiceUrl + '/UpdateQuantity', { skuid: skuid, quantity: quantity })
.then(function (response) {
//$scope.cart = response.data.d; -- this is the issue
console.log(response.data.d);
$scope.calculateTotalWithDonation();
});
};
答案 0 :(得分:0)
我在Angular的Github上找到了答案。答案是使用跟踪(你需要v1.3 +)。
<div class="cart-items" ng-repeat="item in cart.Items track by $index">
<div class="cart-item">
<div class="cart-item-quantity">
<input keep-focus type="text" maxlength="3" ng-model="cart.Items[$index].Quantity" ng-change="update(cart.Items[$index].SKUID, cart.Items[$index].Quantity)" ng-blur="update(cart.Items[$index].SKUID, cart.Items[$index].Quantity)"/>
</div>
</div>
</div>