微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我必须将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;       
}]);

原文地址:https://www.jb51.cc/angularjs/143256.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐