如何在Angular中调用$资源

时间:2016-08-25 18:19:03

标签: javascript angularjs

我有以下代码:

控制器有:

    $scope.buscarTermos = function (termo, campo){
        if(termo.length>1){
           $scope.legendas = Legenda.query({'termo':'Luiz', 'campo':'legenda'}, function(todo) {
        });
        }
    }

和工厂

 .factory('Legenda', ['$resource',
    function ($resource) {
        return $resource(apiDomain + 'AutoComplete/', {
                    'termo': '@Termo',
                    'campo': '@Campo'
        });
    }])

如何让$ buscarTermos工作?当我调用它时,$ scope.legendas没有任何反应只是加载一个承诺......我如何执行承诺?

1 个答案:

答案 0 :(得分:0)

我刚刚找到了答案!

$scope.buscarTermos = function (termo, campo){
        if(termo.length>1){
           $scope.legendas = Legenda.query({'termo':'Luiz', 'campo':'legenda'});

           $q.all([$scope.legendas.$promise]).then( function(result) {
                        $scope.legendas = result;
                        console.log($scope.legendas);
           })
        }
    }

我只需要一个$ q里面!谢谢伙计们!