AngularJS:从一个工厂,我怎么能调用另一个函数 [英] AngularJS : From a factory, how can I call another function

查看:356
本文介绍了AngularJS:从一个工厂,我怎么能调用另一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将我getTemplates功能了回报还是什么?

例如:我不知道,以替换XXXXXXX与(我已经试过这个/自/ templateFactory等):

  .factory('templateFactory',[
    $ HTTP,
    功能($ HTTP){        变种模板= [];        返回{
            getTemplates:功能(){
                $ HTTP
                    获得('../ API / index.php文件/路径/ templates.json')
                    .success(功能(数据){
                        模板=数据;
                    });
                返回模板;
            },
            删除:函数(ID){
                $ http.delete('../ API / index.php文件/路径/模板/+ ID +'以.json)
                .success(函数(){
                    模板= XXXXXXX.getTemplates();
                });
            }
        };
    }
])


解决方案

这样做模板= this.getTemplates(); 你指的是一个对象的属性是不但实例化。

相反,你可以慢慢填充对象:

  .factory('templateFactory',['$ HTTP',函数($ HTTP){
    变种模板= [];
    变种物镜= {};
    obj.getTemplates =功能(){
        $ http.get('../ API / index.php文件/路径/ templates.json')
            .success(功能(数据){
                模板=数据;
            });
        返回模板;
    }
    obj.delete =功能(ID){
        $ http.delete('../ API / index.php文件/路径/模板/+ ID +'以.json)
            .success(函数(){
                模板= obj.getTemplates();
            });
    }
    返回OBJ;
}]);

Do I have to move my getTemplates function out of the return or what ?

Example : I dont know what to replace "XXXXXXX" with (I have tried "this/self/templateFactory" etc... ) :

.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();
                });
            }
        };
    }
])

解决方案

By doing templates = this.getTemplates(); you are referring to an object property that is not yet instantiated.

Instead you can gradually populate the object:

.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;       
}]);

这篇关于AngularJS:从一个工厂,我怎么能调用另一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆