[英]Angularjs $scope variables not updating in view
I'm new to Angularjs and im trying to update two $scope variables in two seperate functions which are called on ng-click. 我是Angularjs的新手,我试图在两个单独的函数中更新两个$ scope变量,这些函数在ng-click上调用。
Even though the variables update, they wont rebind in the view. 即使变量更新,它们也不会在视图中重新绑定。
HTML HTML
<div ng-app="">
<div ng-controller="MainCtrl">
<p> <a href="#" ng-click="getDetails();getElse()">Refresh</a> </p>
<p ng-controller="MainCtrl"><span ng-bind="something"></span></p>
<p ng-controller="MainCtrl"><span ng-bind="somethingelse"></span></p>
</div>
</div>
JS function MainCtrl($scope) { JS函数MainCtrl($ scope){
$scope.something = "something";
$scope.somethingelse = "else";
$scope.getDetails = function () {
alert("getdetails before change: "+$scope.something);
$scope.something = 'changed';
alert("getdetails: "+$scope.something);
};
$scope.getElse = function () {
alert("getElse before change: "+$scope.somethingelse);
$scope.somethingelse = 'changed';
alert("getElse: "+$scope.somethingelse);
};
}
I've created a fiddle to show you what i mean: http://jsfiddle.net/M3pZ8/ 我创造了一个小提琴来告诉你我的意思: http : //jsfiddle.net/M3pZ8/
can anyone tell me what is the correct way to do this? 谁能告诉我这是怎么回事?
Thanks in advance. 提前致谢。
It's because you have MainCtrl
declared 3 times, effectively creating 3 separate scopes. 这是因为您将MainCtrl
声明了3次,有效地创建了3个独立的范围。 You only need it once, at the top. 你只需要一次,在顶部。
<div ng-controller="MainCtrl">
<p> <a href="#" ng-click="getDetails();getElse()">Refresh</a> </p>
<p><span ng-bind="something"></span></p>
<p><span ng-bind="somethingelse"></span></p>
</div>
更新了你的jsfiddle
don't need to add same controller multiple times.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.