AngualrJS - 控制器知名度 [英] AngualrJS - controller visibility
问题描述
我移植了一些问题形式的角度来1.2.9 1.4.9。我做很多变化,我的code,现在我不明白为什么在控制器中声明的函数是不是其他的控制器看到了,我在哪里错了?
在细节,这是错误:
的ReferenceError:getCookie方法没有定义
块引用>而code感兴趣的是这个
JS:
scotchApp
.controller('setCookie方法',函数(CNAME,cvalue,exdays){
变种D =新的日期();
d.setTime(d.getTime()+(exdays * 24 * 60 * 60 * 1000));
变种到期=过期=+ d.toUTCString();
的document.cookie = CNAME +=+ cvalue +,+届满;
});
//结束保存Cookie//为读取全局变量
scotchApp
.controller('的getCookie',函数(CNAME){
变量名称= CNAME +=;
变种CA = document.cookie.split(';');
对于(VAR I = 0; I< ca.length;我++){
变种C = CA [I]
而(c.charAt(0)=='')C = c.substring(1);
如果(c.indexOf(名称)== 0)找到return c.substring(name.length,c.length);
}
返回;
});
//结束读取CookiescotchApp
.controller('ControllerForm',[$范围,$窗口,$ HTTP,Base64编码功能($范围,$窗口,$ HTTP,Base64编码){ $ http.get(/ Activiti的静止/服务/运行/任务)//每prendermi L'ID德尔processo necessario
。然后(功能(响应,数据,身份,头,配置){
变种currentProcessId =的getCookie(currentProcessId); {} ........
});
}]);
解决方案您应该定义与设置/获取的饼干暴露方法的工厂。当然不需要做那么控制器。这里有一个例子
scotchApp
.factory('cookieFactory',[功能(){
返回{
setCookie方法:功能(CNAME,cvalue,exdays){
变种D =新的日期();
d.setTime(d.getTime()+(exdays * 24 * 60 * 60 * 1000));
变种到期=过期=+ d.toUTCString();
的document.cookie = CNAME +=+ cvalue +,+届满;
}, getCookie方法:功能(CNAME){
变量名称= CNAME +=;
变种CA = document.cookie.split(';');
对于(VAR I = 0; I< ca.length;我++){
变种C = CA [I]
而(c.charAt(0)=='')C = c.substring(1);
如果(c.indexOf(名称)== 0)找到return c.substring(name.length,c.length);
}
返回;
}
}
}]);注入
cookieFactory
在你的控制器,然后访问这些方法scotchApp
.controller('ControllerForm',[$范围,$窗口,$ HTTP,Base64编码
cookieFactory
功能($范围,$窗口,$ HTTP,Base64编码,
cookieFactory){ $ http.get(/ Activiti的静止/服务/运行/任务)//每prendermi L'ID德尔processo necessario
。然后(功能(响应,数据,身份,头,配置){
变种currentProcessId = cookieFactory.getCookie(currentProcessId); {} ........
});
}]);I migrated with some problem form angular 1.2.9 to 1.4.9. I do many changes in my code and now i dont understand why a function declared in a controller isn't visible to other controller anymore, where am i wrong?
In detail this is the error:
ReferenceError: getCookie is not defined
And the code interested is this
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"); {........} }); }]);
解决方案You should define a factory with exposed method which set/get's cookies. Surely don't need to make then controllers. Here's an example
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 ""; } } }]);
Inject the
cookieFactory
in your controller then access these methodsscotchApp .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"); {........} }); }]);
这篇关于AngualrJS - 控制器知名度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!