Angularjs 返回数组的角度提供程序
我试图创建一个Angularjs 返回数组的角度提供程序,angularjs,Angularjs,我试图创建一个CustomRouteProvider,但当我在控制器中请求customerRoutes时,数组总是空的 angular.module('introToAngularApp') .config(function(customRoutesProvider) { customRoutesProvider.setRoutes([ /* routes */ ]) }); .provider('customRoutes', function() {
CustomRouteProvider
,但当我在控制器中请求customerRoutes
时,数组总是空的
angular.module('introToAngularApp')
.config(function(customRoutesProvider) {
customRoutesProvider.setRoutes([ /* routes */ ])
});
.provider('customRoutes', function() {
this.customRoutes = [];
this.setRoutes = function (routes) {
this.customRoutes = routes;
};
this.getRoutes = function() {
return customRoutes;
};
// Method for instantiating
this.$get = function() {
var customRoutes = this.customRoutes;
return {
getRoutes: function() {
return customRoutes;
}
};
};
})
.controller('DemoCtrl', function(customRoutes) {
console.log(customRoutes.getRoutes()); // []
});
我甚至尝试将单个路由推到this.customRoutes
中,而不是仅仅分配它
有谁有更好的方法来实现这一点吗?你的
这个里面的$this.get
-getRoutes
与它的父项不同。
您需要访问的不是这个
,而是那个
,\u这个
,或者其他什么
.provider('customRoutes', function() {
this.customRoutes = [];
this.setRoutes = function (routes) {
this.customRoutes = routes;
};
this.getRoutes = function() {
return customRoutes;
};
// Method for instantiating
var _this = this;
this.$get = function() {
return {
getRoutes: function() {
return _this.customRoutes;
}
};
};
})
plnkr这里,你能用一个小例子来复制它吗。这里的工作似乎很好,猜猜发布代码中的打字错误代码>在正确过帐时是意外的。在实际代码中,是否在配置块正确之前定义了提供程序?