Ich bin verwirrt oder kann nicht verstanden, wie eckige Versprechen funktioniert. Ich versuche, einige Codeblöcke für meine Fehlerbehandlung zu schreiben, aber ich sehe, dass es immer Erfolgsblock in meinem Controller ausführt. Allerdings habe ich auch Erfolg und Fehler in meinem Service geschrieben, weil ich eine Transformation in meiner Antwort brauche. Ich sehe, dass es einen Fehlerblock im Dienst ausführt, was vollkommen in Ordnung ist, aber das gleiche Versprechen führt einen Erfolgsblock in meinem Controller aus.
HTML
<div ng-app="myApp">
<div ng-controller="MainController">
<h1>{{data}}</h1>
</div>
<div>
JS
angular.module('services', []).service('myService', function($http) {
this.getData = function() {
return $http.get('test.json').then(function (response) {
console.log(response);
return response.data;
},function(data) {
console.log("Error block of service");
});
}
});
var app = angular.module('myApp', ['services']);
app.controller('MainController', ['$scope', 'myService', function ($scope, myService) {
// Call the getData and set the response "data" in your scope.
myService.getData().then(function(myReponseData) {
console.log("Success block of controller");
$scope.data = myReponseData;
},function(data) {
console.log("Error block of controller");
$scope.data = "Error " + data;
});
}]);
ich reproduzieren das gleiche Problem in der Geige. Werfen Sie einen Blick