function - AngularJS : From a factory, 如何调用另一个函数

标签 function angularjs factory

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

最佳答案

通过做 templates = 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;       
}]);

关于function - AngularJS : From a factory, 如何调用另一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18234442/

相关文章:

javascript - AngularJS:fn 不是函数

javascript - angular 中的有效 jq,ether angular.element ("#n") 或 $ ("#n")

java - 检查类型参数是否是特定接口(interface)

jquery - 如何为单个函数引用两个 div 元素?

c++ - 不能将 vector 作为参数传递 c++

javascript - AngularJS Factory 变量值分配打破绑定(bind)

php - PHP 应用程序中的加权负载平衡算法

scala - Scala 中的伴生对象未将自身与案例类关联

c++ - std::function 的类型别名

c - 如何将两个二维数组中的整数添加到用户指定长度的新二维数组中?