我必须将getTemplates函数从返回中移出还是什么?
示例:我不知道用“XXXXXXX”替换(我尝试过“this / self / templateFactory”等)…):
- .factory('templateFactory',[
- '$http',function($http) {
- var templates = [];
- return {
- getTemplates : function () {
- $http
- .get('../api/index.PHP/path/templates.json')
- .success ( function (data) {
- templates = data;
- });
- return templates;
- },delete : function (id) {
- $http.delete('../api/index.PHP/path/templates/' + id + '.json')
- .success(function() {
- templates = XXXXXXX.getTemplates();
- });
- }
- };
- }
- ])
通过做模板= this.getTemplates();您指的是尚未实例化的对象属性.
相反,您可以逐渐填充对象:
- .factory('templateFactory',['$http',function($http) {
- var templates = [];
- var obj = {};
- obj.getTemplates = function(){
- $http.get('../api/index.PHP/path/templates.json')
- .success ( function (data) {
- templates = data;
- });
- return templates;
- }
- obj.delete = function (id) {
- $http.delete('../api/index.PHP/path/templates/' + id + '.json')
- .success(function() {
- templates = obj.getTemplates();
- });
- }
- return obj;
- }]);