【发布时间】:2016-11-08 08:43:49
【问题描述】:
我是 angularJS 的新手,我不知道如何将 $watch 添加到特定模型。在阅读 angularjs 教程时,我遇到了一些问题。我在 cmets 部分提到了我的疑问。请通过这个。
(function(angular) {
angular.module('controllerAsExample', [])
.controller('SettingsController1', SettingsController1);
function SettingsController1() {
this.name = "John Smith";
this.contacts = [
{type: 'phone', value: '408 555 1212'},
{type: 'email', value: 'john.smith@example.org'} ];
}
//how to add $watch to ng-model 'settings.name'
/*$scope.$watch("settings.name", function(oldval, newval){
console.log(oldval + " + " + newval);
});*/
SettingsController1.prototype.greet = function() {
console.log(this.name);
};
})(window.angular);
HTML 代码..
<body ng-app="controllerAsExample">
<div id="ctrl-as-exmpl" ng-controller="SettingsController1 as settings">
<label>Name: <input type="text" ng-model="settings.name"/></label>
<button ng-click="settings.greet()">greet</button><br/>
</div>
</body>
在这里查看我的link
【问题讨论】:
-
为什么要添加 $watch?
标签: javascript angularjs