I'm trying to modify a variable inside $scope within a controller's function and the affectation works, but didn't apply in the view.
我试图在控制器的函数中修改$scope中的一个变量,这个伪函数起作用,但在视图中没有应用。
app.controller('LoginCtrl', ['$scope', '$http', function ($scope, $http) {
$scope.test = "test";
$scope.logIn = function() {
// This changes the value to test2, but does not applies it to the view.
$scope.test = "test2";
};
}]);
In the login.html:
login.html:
DEBUG: {{ test }}
And the routeProvider:
routeProvider:
$routeProvider
.when('/', {
templateUrl: '/views/login.html',
controller: 'LoginCtrl'
})
Is something I miss out the documentation ?
我是不是漏掉了文档?
Thanks !
谢谢!
3 个解决方案
#1
0
So I think this is a reference or inheritance issue that is known with primitives see this post https://github.com/angular/angular.js/wiki/Understanding-Scopes. Try this same code but put test inside an object like so:
所以我认为这是一个引用或继承问题,对于原语来说是众所周知的,请参阅本文https://github.com/angular/angular.js/wiki/understand - scope。尝试同样的代码,但是将测试放在一个对象中,比如:
$scope.viewModel = {};
$scope.viewModel.test = 'Test';
#2
3
This happens because angular is not aware of the scope changes made by logIn function which i think is called asynchronously in your case. After calling logIn() asynchronously, force scope changes by
之所以会出现这种情况,是因为angle不知道登录函数所做的范围更改,我认为这在您的例子中是异步调用的。异步调用logIn()之后,强制作用域发生变化
$scope.$apply()
#3
0
I found out that I put ng-controller="" in the HTML element that calls the logIn() function.
我发现在调用logIn()函数的HTML元素中放入了ng-controller="" "。
In fact, I called it twice and causes the view update to fail.
实际上,我调用了它两次,导致视图更新失败。
#1
0
So I think this is a reference or inheritance issue that is known with primitives see this post https://github.com/angular/angular.js/wiki/Understanding-Scopes. Try this same code but put test inside an object like so:
所以我认为这是一个引用或继承问题,对于原语来说是众所周知的,请参阅本文https://github.com/angular/angular.js/wiki/understand - scope。尝试同样的代码,但是将测试放在一个对象中,比如:
$scope.viewModel = {};
$scope.viewModel.test = 'Test';
#2
3
This happens because angular is not aware of the scope changes made by logIn function which i think is called asynchronously in your case. After calling logIn() asynchronously, force scope changes by
之所以会出现这种情况,是因为angle不知道登录函数所做的范围更改,我认为这在您的例子中是异步调用的。异步调用logIn()之后,强制作用域发生变化
$scope.$apply()
#3
0
I found out that I put ng-controller="" in the HTML element that calls the logIn() function.
我发现在调用logIn()函数的HTML元素中放入了ng-controller="" "。
In fact, I called it twice and causes the view update to fail.
实际上,我调用了它两次,导致视图更新失败。