gpt4 book ai didi

javascript - AngularJS - 范围未应用于 Controller 功能中的 View

转载 作者:行者123 更新时间:2023-11-29 19:30:53 26 4
gpt4 key购买 nike

我正在尝试在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com