【发布时间】:2016-02-07 22:26:50
【问题描述】:
我一直在寻找,到目前为止我找不到处理以下情况的方法:
我想要一个代表我的域的变量,例如http://example.com/myserver/。我想要访问的任何内容都在该域中进行了子本地化。在所有Services 以及 HTML(用于图像加载目的)中,我都应该可以使用这样的变量。例如:
angular.module('App', ['App.providers', 'App.services', 'App.controllers', 'App.directives']).run('EndpointFactory', [function (EndpointFactory) {
EndpointFactory.BASE = 'http://www.example.com/mydata';
}]);
angular.module('App.services', []).factory('PrizeService', ['$http', 'EndpointFactory', function ($http, EndpointFactory) {
return {
index: function () {
return $http.post(EndpointFactory.BASE + '/prizes', {});
}
};
}]);
上面的这个例子是我失败的试验之一。虽然我不喜欢必须在每个 Service 中注入 Factory 的想法,但我愿意这样做,直到遇到 <img ng-src /> 标签。
<div>
<img ng-src="{{EndpointFactory.BASE + prize.imagePath}}" />
</div>
我无法从此指令访问 EndpointFactory,我不想将数据从工厂复制到每个控制器中的 $scope。
【问题讨论】:
标签: javascript html angularjs angularjs-scope angularjs-factory