0
因此,我有两个控制器(它们已被简化为简化),每个控制器都有一个函数。这些功能在每个控制器内改变一个变量。事情是,它看起来只有第一个控制器中的变量被更新。变量不更新外部函数
app.js
(function() {
var app = angular.module('ScBO', []);
var token = {};
app.controller("LoginController", ['$http', function($http){
this.userData = {};
var lc = this;
this.in = false;
this.loginGo = function(){
$http.post(<link>, <userData>)
.then(function successCallback(response){
token = response.data.access_token;
lc.in=true;
}, function errorCallback(response){
lc.in=false;
});
};
}]);
app.controller("DashboardController", ['$http', function($http){
var dsb = this;
this.totalLocals = 0;
this.refresh = function(){
$http.get('<link>?access_token='+token)
.then(function successCallback(response){
dsb.totalLocals = response.data.number_of_locals.number_of_locals;
}, function errorCallback(response){
});
};
}]);
})();
的index.html
<body ng-controller="LoginController as loginCtrl">
<div id="login-div" ng-hide="loginCtrl.in">
<form ...>
...
</form>
</div>
<div id="dashboard" ng-show="loginCtrl.in" ng-controller="DashboardController as dsb">
<div class="numbers">
<p>Number of Locals</p>
{{dsb.totalLocals}}
</div>
</div>
</body>
所以在HTML中,第一个div出现在开端,而不是第二个。然后它隐藏起来,第二个div出现。这意味着他们正在关注loginCtrl.in变量的更新。 但是,当我调用刷新函数时,它不会更新最后一个div中的值。
我已经尝试过$范围,并一直在这里搜索,但我还没有找到解决方案。特别是因为两个控制器是平等的,但其中只有一个似乎正常更新变量。
你确定你的'$ http' req解决了吗? – Rahul
我做了这个plunkr试图模拟你的问题,对我来说它似乎工作:https://plnkr.co/edit/ySNl3t17vKMgaRKn8LR6?p=preview – leonardoborges
@Rahul我试着把一个console.log(response.data .number_of_locals.number_of_locals);在分配给变量dsb.totalLocals之后,看到来自服务器的值是正确的。 – Johnny