我手上拿着一个特殊的箱子。我在我的角度应用程序中使用了input[type="number"]
的数字填充。现在,问题是,它没有考虑到安古拉杰的处理方式。
类似地,如果我以编程方式增加/减少输入值,则与输入关联的ngModel将不会得到更新的值。
我已经通过编写自定义指令并将模型传递到polly填充来解决这个问题。
angular.module('appModule').
directive('type', ['$parse', '$timeout', function($parse, $timeout) {
restrict: 'A',
require: '?ngModel',
link: function($scope, iElement, iAttrs, ngModel) {
...
if iAttrs.type is 'number'
iElement.inputNumber(ngModel) // <-- THIS LINE
...
}
}
在多填充代码中,我修改了以下内容:
increment = function(elem) {
...
if (model != null) {
model.$setViewValue(newVal);
}
...
}
效果很好。现在,问题是即使在更新模型的值之后,关联的表单也不会变成脏的。在我的代码中,不可能将表单作为参数传递到这个填充中。
我试过使用model.$dirty = true
,但这不起作用。
还有别的办法吗?
发布于 2013-08-29 06:17:57
我发现了错误。虽然$setViewValue
应该将我的表单设置为脏,但阻止它的是我所做的代码是out-of-$scope
;)
因此,$scope.$apply()
就派上了用场。因此,现在,随着模型,我也传递给$scope的多边形填充。
angular.module('appModule').
directive('type', ['$parse', '$timeout', function($parse, $timeout) {
restrict: 'A',
require: '?ngModel',
link: function($scope, iElement, iAttrs, ngModel) {
...
if iAttrs.type is 'number'
iElement.inputNumber(ngModel, $scope) // <-- THIS LINE
...
}
}
increment = function(elem) {
...
if (model != null) {
$scope.$apply(function() {
model.$setViewValue(newVal);
}
}
...
}
https://stackoverflow.com/questions/18512478
复制