angularjs - Angular,尝试使用服务时出现 "x is not a function"

标签 angularjs function service factory

app.service('situacao', function($log, $q, $http, $rootScope){
    var situacao = this; 
    situacao.lista = {};
    situacao.getAllSituacao = function(){
        var defer = $q.defer();
        console.log("php/getAll.php");
        $http.get($rootScope.endPoint + "php/getAll.php")
            .success(function(res) {
            console.log(res);
            situacao.lista = res;
            defer.resolve(res);
        }).error(function(err, status){
            defer.reject(err);
        }); 
        return defer.promise;
    };
    return situacao;});
    app.controller('listCtrl',['$scope', '$uibModal', '$log', '$http', function(situacao, $scope, $modal, $log, $http) {   
    $scope.init = function(){
        $scope.getAll();
    }
    $scope.getAll = function(){        
        situacao.getAllSituacao().then(function(res){
            //sucess
            $scope.dispSituacao = situacao.lista;  
        }, function(err){
            //error
        })
    };
    $scope.init(); 
}]);

我尝试使用“服务”但出现错误: situacao.getAllSituacao 不是函数。

出了什么问题?

最佳答案

由于您使用的是数组表示法,因此您必须更新注入(inject)才能将其传入:

改变

app.controller('listCtrl', ['$scope', '$uibModal', '$log', '$http', function (situacao, $scope, $modal, $log, $http)

app.controller('listCtrl', ['situacao', '$scope', '$uibModal', '$log', '$http', function (situacao, $scope, $modal, $log, $http) {

关于angularjs - Angular,尝试使用服务时出现 "x is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36087417/

相关文章:

javascript - $watch vs 花括号 - $watch 不捕获范围内变量的变化

angularjs - 有效识别元素 - AngularJS Automation with Ranorex

javascript - 构建计算器,获取 Cannot set property 'value' of null

angular - 如何在我的 Angular 2 应用程序中保持对话框的状态和进度?

android - 如何在 Android Wear 中永久运行服务

javascript - 在 Controller 中使用 'var' 声明的变量,但在 Controller 中作为作用域引用

angularjs - 具有开放层的 Angular JS

java - 是否有任何好的函数库可用于 Java 中的集合,例如

oracle - 尝试创建PL/SQL函数时出现密码错误

c# - 为什么我们不能像使用 WCF 或 ASMX 一样在 Visual Studio 中将 Web API 添加为 "service reference"?