javascript - Angular JS "Controller as"语法不起作用

标签 javascript angularjs angularjs-scope angularjs-controller

我之前建过一个AngularJS的项目,语法比较熟悉。这一次我的 ng-controller="UniversalCtrl as universal" 没有工作,我已经尝试了一切。如果我采用 universal.showHeader == true 并将其更改为 showHeader == true 它可以工作,但我需要它是 universal 的变量>。就像我说的,我还有其他项目遵循相同的结构,并且它们运行良好。

这是我的 HTML 代码:

<!DOCTYPE html>
<html>
    <head lang="en">
       <meta http-equiv="cache-control" content="no-store" />
       <meta http-equiv="expires" content="0" />
       <meta http-equiv="X-UA-Compatible" content="IE=edge" />
       <meta http-equiv="pragma" content="no-store" />
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
       <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes">

       <link href="styles/MarkStrap.css" rel="stylesheet" />
       <link href="styles/Site.css" rel="stylesheet" />

        <script type="text/javascript" src="js/Angular/angular.min.js"></script>
        <script type="text/javascript" src="js/Angular/angular-route.min.js"></script>
        <script type="text/javascript" src="js/Universal/Universal.js"></script>
        <script type="text/javascript" src="js/Universal/Filters.js"></script>
        <script type="text/javascript" src="js/Universal/Directives.js"></script>

        <title>WIN</title>
        <link rel="shortcut icon" href="winIcon.ico">
    </head>
    <body ng-app="winApp" ng-controller="UniversalCtrl as universal">

       <index-header ng-show="universal.showHeader == true"></index-header>
       <ng-view></ng-view>

       <script type="text/javascript" src="js/Applications/Applications.js"></script>
    </body>
</html>

这是我的 Universal.js 设置:

(function () {
    var winSystem = angular.module('winApp', ['ngRoute']);

    winSystem.config(function ($sceProvider, $routeProvider) {
        $routeProvider
                .when('/Applications', {
                    templateUrl: "view-app.html",
                    controller: "AppController"
                })
                .otherwise({
                    templateUrl: "404.html"
                })
    });

    winSystem.service("sharedData", function () {
        var reloadData = false;
        var beginAppLoad = false;
        var reloadNotes = false;

        self.httpGet = function (http, url, callback) {
            http.get(baseUrl + url, {
                headers: { "Session-Id": localStorage.ogSessionId }
            }).success(function (data) {
                callback(data);
            }).error(function (data, status, headers, config) {
                if (status == "401") {
                    localStorage.removeItem("ogSessionId");
                    localStorage.removeItem("ogUserId");
                    window.location.href = loginUrl;
                }
            });
        };

        self.httpPost = function (http, url, content, callback) {
            http.post(baseUrl + url, {
                Content: JSON.stringify(content)
            }, {
                headers: {
                    "Session-Id": localStorage.ogSessionId
                }
            })
                .success(function (data) {
                    callback(data);
                })
                .error(function (data, status, headers, config) {
                    if (status == "401") {
                        localStorage.removeItem("ogSessionId");
                        localStorage.removeItem("ogUserId");
                        window.location.href = loginUrl;
                    }
                });
        };


    });

    winSystem.controller("UniversalCtrl", ['$scope', '$http', 'sharedData', function ($scope, $http, sharedData) {
        var self = $scope;

        self.sharedDataSvc = sharedData;
        self.isLocal = false;

        if (location.href.indexOf("localhost") > -1) {
            self.isLocal = true;
        } else self.isLocal = false;

        self.account = {};
        self.actions = [];
        self.notifications = [];

        self.alertCount = 0;
        self.showAlert = false;
        self.showHeader = true;
        self.alertMessage = "";

    }]);
})();

最佳答案

您将模型绑定(bind)到范围对象而不是 Controller 实例。 尝试:

winSystem.controller("UniversalCtrl", ['$http', 'sharedData', function ($http, sharedData) {
    var self = this;

    self.sharedDataSvc = sharedData;
    self.isLocal = false;

    if (location.href.indexOf("localhost") > -1) {
        self.isLocal = true;
    } else self.isLocal = false;

    self.account = {};
    self.actions = [];
    self.notifications = [];

    self.alertCount = 0;
    self.showAlert = false;
    self.showHeader = true;
    self.alertMessage = "";

}]);

而且我注意到您在服务 sharedData 上使用了 self 变量,但是您还没有初始化它。同样的 'var self = this;'

关于javascript - Angular JS "Controller as"语法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29370723/

相关文章:

javascript - JSON 模式 : represent constraint to encode object type in string type

javascript - forEach 函数比等效的 for 循环快得多

javascript - Angular 服务数据未在 $scope 中更新

javascript - 移动设备:从 anchor 打开多个新选项卡 (__blank)

javascript - Firebase Firestore : orderBy combined with where causes error "Operation was rejected"

javascript - 将 borderRadius 传递给react-native

AngularJS 模块声明的最佳实践?

javascript - 如何让 $index 命名变量在 Angular 中工作?

javascript - 如何在指令的 Controller 中访问父 Controller 的范围?

javascript - ng 在数组上重复以过滤另一个对象上的键以获取关联的值并对该值使用 watch