具有延迟的 ng-change 更新模型
ng-change update model with latency
各位。
我在 angularjs 上遇到了麻烦。我为 input[type="text"] 创建了自定义指令,并将其作为模型传递给变量。但是 ng-change 事件调用了具有先前变量值的函数。
例子:
状态:0,类型 1,在函数中 - 0。
状态:1,类型 548,功能中 - 1。
State:548,类型 3,在函数 548 中。
我的html:
<div ng-controller="simpleCTRL">
<mr-textfield is-required="true" value="val" title="Minutes" type="text" change="ChangeVal()"></mr-textfield>
<input type="text" ng-model="val" ng-change="ChangeVal()"/>
</div>
</div>
和 js:
<!-- language: lang-js -->
angular.module('SimpleInterestApp', [])
.controller('simpleCTRL', function($scope) {
$scope.val = 0;
$scope.ChangeVal = function() {
console.log($scope.val);
};
})
.directive('mrTextfield', function () {
return {
restrict: 'E',
template: "<div class='textfield'><input type='{{::type}}' ng-required='isRequired' ng-model='value' ng-change='change()'><span class='bar'></span><label>{{::title}}</label></div>",
replace: true,
transclude: true,
scope: {
value:"=",
title:"@",
type:"@",
isRequired:"=",
change:"&"
}
};
});
将 console.log
包裹在 $timeout
内。
$scope.ChangeVal = function() {
$timeout(function() {
console.log($scope.val);
})
};
各位。 我在 angularjs 上遇到了麻烦。我为 input[type="text"] 创建了自定义指令,并将其作为模型传递给变量。但是 ng-change 事件调用了具有先前变量值的函数。 例子: 状态:0,类型 1,在函数中 - 0。 状态:1,类型 548,功能中 - 1。 State:548,类型 3,在函数 548 中。
我的html:
<div ng-controller="simpleCTRL">
<mr-textfield is-required="true" value="val" title="Minutes" type="text" change="ChangeVal()"></mr-textfield>
<input type="text" ng-model="val" ng-change="ChangeVal()"/>
</div>
</div>
和 js:
<!-- language: lang-js -->
angular.module('SimpleInterestApp', [])
.controller('simpleCTRL', function($scope) {
$scope.val = 0;
$scope.ChangeVal = function() {
console.log($scope.val);
};
})
.directive('mrTextfield', function () {
return {
restrict: 'E',
template: "<div class='textfield'><input type='{{::type}}' ng-required='isRequired' ng-model='value' ng-change='change()'><span class='bar'></span><label>{{::title}}</label></div>",
replace: true,
transclude: true,
scope: {
value:"=",
title:"@",
type:"@",
isRequired:"=",
change:"&"
}
};
});
将 console.log
包裹在 $timeout
内。
$scope.ChangeVal = function() {
$timeout(function() {
console.log($scope.val);
})
};