2015-11-04 106 views
23

我建一个工厂来处理功能为我的控制器,但不知何故,在控制器上的功能之一返回一个错误:AngularJS错误.success不是一个函数

Error: Auth.getUser(...).success is not a function @http://localhost:8080/app/controllers/mainCtrl.js:10:1
...

我不知道这是怎么回事在这里,其余的功能似乎工作正常?

主控制器:

angular.module('mainCtrl', []) 
.controller('mainController', function($rootScope, $location, Auth) { 
    var vm = this; 
    vm.loggedIn = Auth.isLoggedIn(); 
    $rootScope.$on('$routeChangeStart', function() { 
     vm.loggedIn = Auth.isLoggedIn(); 
     Auth.getUser() 
      .success(function(data) { 
       vm.user = data; 
      }); 
    }); 
    vm.doLogin = function() { 
     Auth.login(vm.loginData.username, vm.loginData.password) 
      .success(function(data) { 
       $location.path('/users'); 
      }); 
    }; 
}); 
+0

哪个角版本您使用的允许? –

+0

AngularJS v1.4.7,刚安装角度与​​凉亭 –

+3

你应该使用.then()函数。你可以传递2个函数作为参数,第一个用于成功回调(与.success相同),第二个用于错误回调。 –

回答

79

参见 '取消通知' 从$http service documentation

The $http legacy promise methods success and error have been deprecated. Use the standard then method instead.

您可以了解更多关于在documentation about $q这些方法。

+0

tnx在所有,你救了我的生活 –

+2

并非所有的英雄穿披肩。谢谢 –

2

你可以使用然后instate成功:

var personController = function ($scope, personService) { 
    $scope.persons = personService.getAll().then(function (data) { 
     $scope.persons = genericSuccess(data); 
    }); 
    var genericSuccess=function(res) { 
     return res.data; 
    } 
}; 
0

它是个好东西,.success已被弃用反正。我个人从来没有与它和平,因为你问?

object.functionCall(parameters).success(functions (response){ 

if(response!=="failure") 
do this----- 
}); 

我的意思是什么样的成功处理其块内的故障。 改为使用然后而所有逻辑将开始有意义。

0

角版本1.2.x版本,你可以使用.success 角1.6.x版,你会得到错误“.success是不是一个函数”

的解决方案是:将“.success”与“然后”

1

这是代码

Depricated代码

$http( 
{ 
    method: 'POST', 
    url: '/Home/CreateCustomer', /*You URL to post*/ 
    data: $scope.cust /*You data object/class to post*/ 
}).success(function (data, status, headers, config) 
{ 

}).error(function (data, status, headers, config) 
{ 

}); 

代码Angular $http docs

$http(
    { 
     method: 'POST', 
     url: '/Home/CreateCustomer', /*You URL to post*/ 
     data: $scope.cust /*You data object/class to post*/ 
    }).then(function successCallback(response) { 
     // this callback will be called asynchronously 
     // when the response is available 


    }, function errorCallback(response) { 
     // called asynchronously if an error occurs 
     // or server returns response with an error status. 

});