加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

AngularJS:从工厂,我如何调用另一个功能

发布时间:2020-12-17 07:49:54 所属栏目:安全 来源:网络整理
导读:我必须将getTemplates函数从返回中移出还是什么? 示例:我不知道用“XXXXXXX”替换(我尝试过“this / self / templateFactory”等)…): .factory('templateFactory',[ '$http',function($http) { var templates = []; return { getTemplates : function ()
我必须将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;       
}]);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读