我试图在 Controller 的函数中修改 $scope 内的变量,并且效果有效,但不适用于 View 。
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";
};
}]);
在login.html中:
DEBUG: {{ test }}
和路由提供者:
$routeProvider
.when('/', {
templateUrl: '/views/login.html',
controller: 'LoginCtrl'
})
我是否遗漏了文档?
谢谢!
最佳答案
发生这种情况是因为 Angular 不知道 logIn 函数所做的范围更改,我认为在您的情况下是异步调用的。异步调用 logIn() 后,强制范围更改
$scope.$apply()
关于javascript - AngularJS - 范围未应用于 Controller 功能内的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27537383/