angularjs定制指令双向绑定表不工作
问题描述:
我创建了一个自定义的指令,并正在使用双向绑定(=)angularjs定制指令双向绑定表不工作
但我想,当模型指令改为观看控制器的变化。
当用户改变输入时警报应该出现,但警报在开始时只出现一次。
我的JavaScript
var myApp = angular.module('myApp', [])
.controller("myCtrl", function ($scope) {
$scope.test = "myValue";
$scope.$watch('myValue', function() {
alert('hey, myVar has changed!');
});
})
.directive('myDirective', function() {
return {
restrict: 'EA',
scope: {
myModel: '=ngModel'
},
template: '<input ng-model="myModel"/>'
}
});
和HTML
<div ng-app="myApp">
<div ng-controller="myCtrl">{{test}}
<my-directive ng-model="test"></my-directive>
</div>
</div>
答
你正在看错变量?
$scope.$watch('test', function() {
alert('hey, myVar has changed!');
});
答
拼写错误'myValue'和'test'。
$scope.$watch('test', function() {
alert('hey, myVar has changed!');
});