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

标签 javascript angularjs controller scope angularjs-scope

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

相关文章:

javascript - 使用 jQuery 选择*不*包含链接的列表项

javascript - 使用 JavaScript 下载 PDF 文件

model-view-controller - JavaFX 在没有 Controller 的 fxml 中包含 fxml

javascript - 了解 AngularJS Controller 中的依赖注入(inject)

java - 通过 JQuery 向 Java 发送 JSON

javascript - 将新系列添加到 highcharts 堆叠柱形图的顶部

javascript - Excel SelectionChanged 事件未触发

javascript - 在 Angularjs 中的对象数组中分配动态索引

javascript - 如何测试 Angular Directive(指令)范围

java - 如何将对象从一个 Controller 传递到另一个 Controller