Javascript Angularjs:从外部角度更改模型时,将form设置为dirty
我手上有个特殊的案子。在我的Angular应用程序中,我将数字polyfill用于Javascript Angularjs:从外部角度更改模型时,将form设置为dirty,javascript,angularjs,polyfills,Javascript,Angularjs,Polyfills,我手上有个特殊的案子。在我的Angular应用程序中,我将数字polyfill用于输入[type=“number”]。现在,问题是它没有考虑到accound Angularjs处理事情的方式 类似地,如果我以编程方式递增/递减输入值,则与输入关联的ngModel不会获得更新的值 我通过编写一个自定义指令并将模型传递到pollyfill中解决了这个问题 angular.module('appModule'). directive('type', ['$parse', '$timeout', f
输入[type=“number”]
。现在,问题是它没有考虑到accound Angularjs处理事情的方式
类似地,如果我以编程方式递增/递减输入值,则与输入关联的ngModel不会获得更新的值
我通过编写一个自定义指令并将模型传递到pollyfill中解决了这个问题
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
...
}
}
这很好用。现在的问题是,即使在更新模型的值之后,关联的表单也不会变脏。在我的代码中,不可能将表单作为参数传递到此polyfill中
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);
}
}
...
}
我试着使用模型。$dirty=true
,但不起作用
还有别的办法吗?我发现了错误。虽然
$setViewValue
应该将我的表单设置为dirty,但阻止它的是我执行此操作的代码超出了-$scope
;)
而且,对于这一点,这种方法也派上了用场。所以,现在随着模型,我也将$scope传递给polyfill
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);
}
}
...
}
angular.module('appModule')。
指令('type',['$parse','$timeout',函数($parse,$timeout){
限制:“A”,
要求:“?ngModel”,
链接:功能($scope、iElement、iAttrs、ngModel){
...
如果iAttrs.type为“编号”
iElement.inputNumber(ngModel,$scope)//标题中的[Advanced]
是什么意思?不知道如何将其与(相当常见的)区分开来更新表单状态的查询。现在更新了标题。调用$setViewValue
应该会将表单控件设置为dirty。你能发布一个fiddle吗?哦,我发现了错误。你有没有可能分享你对number polyfill的修改?@schmod:这些更改是我在回答中写的。如果你想要完整的更改文件,请让我知道。。。