AngualrJS - 控制器可见性

时间:2016-01-26 10:11:55

标签: javascript angularjs

我迁移了一些问题形式角度1.2.9到1.4.9。我在代码中做了很多更改,现在我不明白为什么在控制器中声明的函数不再对其他控制器可见,我错在哪里?

详细说明这是错误:

  

ReferenceError:未定义getCookie

感兴趣的代码就是这个

JS:

 scotchApp
    .controller('setCookie', function (cname, cvalue, exdays) {
        var d = new Date();
        d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
        var expires = "expires=" + d.toUTCString();
        document.cookie = cname + "=" + cvalue + "; " + expires;
    });
//end save cookie

//for read global variable
scotchApp
    .controller('getCookie', function (cname) {
        var name = cname + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1);
            if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
        }
        return "";
    });
//end read cookie

scotchApp
    .controller('ControllerForm', ["$scope", "$window", "$http", "Base64", function ($scope, $window, $http, Base64) {

        $http.get("/activiti-rest/service/runtime/tasks") //necessario per prendermi l'id del processo
            .then(function (response, data, status, headers, config) {
                var currentProcessId = getCookie("currentProcessId"); 

               {........}
            });
}]);

3 个答案:

答案 0 :(得分:2)

您不应将set/getCookie函数转换为控制器,它对此应用程序没有任何意义。相反,感觉它们应该成为某些可重用服务的一部分:

scotchApp.factory('cookies', function() {
    return {
        set: function() { /*...*/ },
        get: function() { /*...*/ }
    };
});

然后将此服务注入ControllerForm并使用它:

scotchApp
    .controller('ControllerForm', ["$scope", "$window", "$http", "Base64", "cookies", function ($scope, $window, $http, Base64, cookies) {

        $http.get("/activiti-rest/service/runtime/tasks") //necessario per prendermi l'id del processo
            .then(function (response, data, status, headers, config) {
                var currentProcessId = cookies.get("currentProcessId"); 

               {........}
            });
}])

答案 1 :(得分:1)

您应该使用已设置/获取cookie的公开方法定义工厂。当然不需要制作控制器。这是一个例子

scotchApp
    .factory('cookieFactory', [function(){
        return {
            setCookie : function (cname, cvalue, exdays) {
                var d = new Date();
                d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
                var expires = "expires=" + d.toUTCString();
                document.cookie = cname + "=" + cvalue + "; " + expires;
            },

            getCookie: function (cname) {
                var name = cname + "=";
                var ca = document.cookie.split(';');
                for (var i = 0; i < ca.length; i++) {
                    var c = ca[i];
                    while (c.charAt(0) == ' ') c = c.substring(1);
                    if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
                }
                return "";
            }
        }
    }]);

在控制器中注入cookieFactory然后访问这些方法

scotchApp
    .controller('ControllerForm', ["$scope", "$window", "$http", "Base64", 
    "cookieFactory",
    function ($scope, $window, $http, Base64,
        cookieFactory) {

        $http.get("/activiti-rest/service/runtime/tasks") //necessario per prendermi l'id del processo
            .then(function (response, data, status, headers, config) {
                var currentProcessId = cookieFactory.getCookie("currentProcessId"); 

               {........}
            });
}]);

答案 2 :(得分:1)

似乎你的get和set cookie控制器只是函数。最简单的改变就是将它们移到“ControllerForm”中。然后它们将在相同的范围内,并且应该摆脱丢失的参考错误。

scotchApp
    .controller('ControllerForm', ["$scope", "$window", "$http", "Base64", function ($scope, $window, $http, Base64) {

     function getCookie(cname){
     ...
     }

    function setCookie (cname, cvalue, exdays){
     ...
     }

    $http.get("/activiti-rest/service/runtime/tasks") //necessario per prendermi l'id del processo
            .then(function (response, data, status, headers, config) {
                var currentProcessId = getCookie("currentProcessId"); 

               {........}
            });
}]);