我正在创建angular指令,它使用bootstrap表单组包装html输入。我使用ng-change事件来监听更改,但是我在ng-change处理程序中得到旧值。为了显示这一点,我创建了相同的指令,一个使用ng-keyup,另一个使用ng-change事件来监听更改。 / p>
var app = angular.module('app', []);
app.controller('home', function() {
this.textKeyUp = 'KeyUp';
this.textNgChange = 'NgChange';
this.textKeyUpChanged = function() {
console.log('Changed on KeyUp:', this.textKeyUp);
};
this.textNgChangeChanged = function() {
console.log('Changed on NgChange:', this.textNgChange);
};
});
app.directive('apTextKeyUp', function() {
return {
controller: function() {},
controllerAs: 'ctrl',
bindToController: {
model: '=',
change: '&'
},
scope: {},
template: '<input ng-model="ctrl.model" ng-keyup="ctrl.change()" />'
};
});
app.directive('apTextNgChange', function() {
return {
controller: function() {},
controllerAs: 'ctrl',
bindToController: {
model: '=',
change: '&'
},
scope: {},
template: '<input ng-model="ctrl.model" ng-change="ctrl.change()" />'
};
});
<!DOCTYPE html>
<html ng-app="app">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
</head>
<body ng-controller="home as ctrl">
<h3>KeyUp</h3>
<ap-text-key-up model="ctrl.textKeyUp" change="ctrl.textKeyUpChanged()"></ap-text-key-up>
<h3>NgChange</h3>
<ap-text-ng-change model="ctrl.textNgChange" change="ctrl.textNgChangeChanged()"></ap-text-ng-change>
</body>
</html>
两个指令都更新模型值,但textNgChangeChanged处理程序值内部尚未更新。
这是设计的吗?我该如何解决这个问题?
答案 0 :(得分:0)
当使用Primative类型作为ng-model值时,将在更改值时调用ng-change。由于这是一种主要类型,因此该值尚未传播到homeCtroller。如果您将指令传递给引用对象,那么事情就会按预期工作。
var app = angular.module('app', []);
app.controller('home', function() {
this.text = {val: 'ABC'};
this.textChanged = function() {
console.log('Changed:', this.text.val);
};
});
app.directive('apText', function() {
return {
controller: function() {
var vm = this;
vm.onChange = function(){
console.log(vm.model);
vm.change();
};
},
controllerAs: 'ctrl',
bindToController: {
model: '=',
change: '&'
},
scope: true,
template: '<input ng-model="ctrl.model.val" ng-change="ctrl.onChange()" />'
};
});
&#13;
<!DOCTYPE html>
<html ng-app="app">
<head>
<link rel="stylesheet" href="style.css" />
<script src="https://code.angularjs.org/1.5.0-rc.0/angular.js"></script>
<script src="script.js"></script>
</head>
<body ng-controller="home as ctrl">
<ap-text model="ctrl.text" change="ctrl.textChanged()"></ap-text>
</body>
</html>
&#13;
var app = angular.module('app', []);
app.controller('home', function() {
this.text = 'ABC';
this.textChanged = function() {
console.log('Changed:', this.text);
};
});
app.directive('apText', function() {
return {
controller: function() {
var vm = this;
vm.onChange = function(){
console.log(vm.model);
vm.change();
};
},
controllerAs: 'ctrl',
bindToController: {
model: '=',
change: '&'
},
scope: true,
template: '<input ng-model="ctrl.model" ng-change="ctrl.onChange()" />'
};
});
&#13;
<!DOCTYPE html>
<html ng-app="app">
<head>
<link rel="stylesheet" href="style.css" />
<script src="https://code.angularjs.org/1.5.0-rc.0/angular.js"></script>
<script src="script.js"></script>
</head>
<body ng-controller="home as ctrl">
<ap-text model="ctrl.text" change="ctrl.textChanged()"></ap-text>
</body>
</html>
&#13;