如何在 Angular 控制器中将 succesCallback 与 errorCallback 分开
How to separate succesCallback from errorCallback in Angular controller
我有这个代码:
app.factory('clients', ['$http', function ($http) {
var _getClient = function (clientId, callback) {
callback = callback || function () {};
$http({
method: 'GET',
url: '/simple_crm/web/api.php/client/'+clientId
}).then(function (data) {
callback(data);
}, function (error) {
callback(error);
});
};
return {
getClient: _getClient
};
}]);
并在控制器中
app.controller('ClientDetailCtrl', ['$scope', 'clients', '$routeParams', function ($scope, clients, $routeParams) {
$scope.user = {};
clients.getClient($routeParams.clientId,
function (data) {
//The block executes clientId is correct
$scope.user = data;
if (404 === data.status) {
//The block executes when Id does not exist and status page is 404
}
}
);
}]);
此代码正常工作 - hides/shows div 块在页面上,但是否可以隔离代码块错误,例如:
clients.getClient($routeParams.clientId,
function (data) {
//clientId is correct
$scope.user = data;
},
function (data) {
if (404 === data.status) {
//clientId does not exist
}
}
);
有什么可能吗?最佳做法是什么?
This code works properly - hides/shows div blocks on the page, but is
it possible to isolate the block of code error for example:
您可以为错误定义另一个回调,并在错误时调用它
var _getClient = function (clientId, successCallback, errorCallback) {
if (success) {
successCallback();
}
if (error) {
errorCallback()
}
}
Is there anything that is possible and what are the best practices
最好和最简单的方法是在您的工厂中 return 这个 $http promise,这样您就可以在您的控制器中使用所有 $http promise 方法(。then()
或 .success()
或 .error()
) 。您将只使用一个承诺,如果需要,它可以与另一个承诺链接,不会被回调破坏。
工厂:
var _getClient = function (clientId) {
return $http({
method: 'GET',
url: '/simple_crm/web/api.php/client/'+clientId
});
};
控制器:
clients.getClient($routeParams.clientId).then(
function(data) {
//success
},
function(error) {
//error
}
);
我有这个代码:
app.factory('clients', ['$http', function ($http) {
var _getClient = function (clientId, callback) {
callback = callback || function () {};
$http({
method: 'GET',
url: '/simple_crm/web/api.php/client/'+clientId
}).then(function (data) {
callback(data);
}, function (error) {
callback(error);
});
};
return {
getClient: _getClient
};
}]);
并在控制器中
app.controller('ClientDetailCtrl', ['$scope', 'clients', '$routeParams', function ($scope, clients, $routeParams) {
$scope.user = {};
clients.getClient($routeParams.clientId,
function (data) {
//The block executes clientId is correct
$scope.user = data;
if (404 === data.status) {
//The block executes when Id does not exist and status page is 404
}
}
);
}]);
此代码正常工作 - hides/shows div 块在页面上,但是否可以隔离代码块错误,例如:
clients.getClient($routeParams.clientId,
function (data) {
//clientId is correct
$scope.user = data;
},
function (data) {
if (404 === data.status) {
//clientId does not exist
}
}
);
有什么可能吗?最佳做法是什么?
This code works properly - hides/shows div blocks on the page, but is it possible to isolate the block of code error for example:
您可以为错误定义另一个回调,并在错误时调用它
var _getClient = function (clientId, successCallback, errorCallback) {
if (success) {
successCallback();
}
if (error) {
errorCallback()
}
}
Is there anything that is possible and what are the best practices
最好和最简单的方法是在您的工厂中 return 这个 $http promise,这样您就可以在您的控制器中使用所有 $http promise 方法(。then()
或 .success()
或 .error()
) 。您将只使用一个承诺,如果需要,它可以与另一个承诺链接,不会被回调破坏。
工厂:
var _getClient = function (clientId) {
return $http({
method: 'GET',
url: '/simple_crm/web/api.php/client/'+clientId
});
};
控制器:
clients.getClient($routeParams.clientId).then(
function(data) {
//success
},
function(error) {
//error
}
);