我有这个工厂:
factory('getJson', ['$resource', function($resource) {
return $resource('json/greetings.json', {}, {
query: {method:'GET', isArray:true}
});
}]);
该文件是硬编码的,'greetings.json',我希望工厂根据我 View 中的复选框获取文件:
<li><input type="checkbox" ng-model="includeVeggies" />Veggies</li>
<li><input type="checkbox" ng-model="includeGreetings" />Greetings</li>
知道我该怎么做吗?
最佳答案
您可以返回一个函数:
.factory('getJson', ['$resource', function($resource) {
return function (file) {
return $resource(file, {}, {
query: {method:'GET', isArray:true}
});
};
}]);
然后在您的 Controller 中:
.controller('MyController', ['getJson', function (getJson) {
getJson('json/greetings.json');
});
Here's a Plnkr .
关于AngularJS 将变量传递给服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25141341/