gol*_*cks 4 angularjs angularjs-service angularjs-resource
我有这个工厂:
factory('getJson', ['$resource', function($resource) {
return $resource('json/greetings.json', {}, {
query: {method:'GET', isArray:true}
});
}]);
Run Code Online (Sandbox Code Playgroud)
该文件是硬编码的'greetings.json',我希望工厂根据我视图中的复选框获取文件:
<li><input type="checkbox" ng-model="includeVeggies" />Veggies</li>
<li><input type="checkbox" ng-model="includeGreetings" />Greetings</li>
Run Code Online (Sandbox Code Playgroud)
知道我该怎么做?
你可以返回一个函数:
.factory('getJson', ['$resource', function($resource) {
return function (file) {
return $resource(file, {}, {
query: {method:'GET', isArray:true}
});
};
}]);
Run Code Online (Sandbox Code Playgroud)
然后在你的控制器中:
.controller('MyController', ['getJson', function (getJson) {
getJson('json/greetings.json');
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4822 次 |
| 最近记录: |