当更改LastMessage时,我需要如何保持消息更新? 预先感谢您。使用ng模型更新函数
angular.module('myApp',[]).controller('MessageController', function($scope) {
getMessage = function(x){
return "Hello, " + x + ":)";
}
$scope.lastMessage = "StackOverflow";
$scope.message = getMessage($scope.lastMessage);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
<div ng-controller="MessageController">
<input type="text" ng-model="lastMessage" ng-model-options="{allowInvalid: true}">
<p ng-bind="lastMessage"></p>
<p ng-bind="message"></p>
</div>
</body>
非常感谢! 在这个例子中,它试图将它转移到我的项目中$ scope调用变量的函数集。 谢谢大家:) – Bellatrix988
很高兴我有一些帮助。请注意,使用太多的$ watch会降低应用程序的速度。另外,尝试转向Angular 2。 – TheVillageIdiot
为避免使用'$ watch'减慢应用速度,请考虑在输入元素上使用[ng-change指令](https://docs.angularjs.org/api/ng/directive/ngChange)。 – georgeawg