很抱歉,如果这是一个愚蠢的问题,但这是我第一次使用Angular和承诺。
我刚刚完成了一个教程,这是我的代码的一部分。
<div ng-app="app">
<div ng-controller="MainController">
<a href ng-click="updateName()">Update Name</a>
<p><strong>Name:</strong> {{name}}</p>
</div>
</div>
var app = angular.module('app', []);
app.factory('NameService', function($http, $q) {
function NameService() {
var self = this;
self.name = null;
var deferred = $q.defer();
if(self.name !== null) {
deferred.resolve(self.name + " (from Cache!)");
} else {
$http.get("xxx.php")
.success(function(name) {
//Here
console.log(name.xxx[0].xxx)
self.name = name.xxx[0].xxx
deferred.resolve(name + " (from Server!)");
.error(function(response) {
deferred.reject(response);
});
}
return deferred.promise
}
}
return new NameService();
});
app.controller('MainController', function ($scope, NameService) {
$scope.name = "";
//$scope.updateName = function() {
NameService.getName()
.then(
function(name) {
$scope.name = name;
},
function(result) {
console.log("Failed to get the name, result is " + result);
});
//};
});
我的控制台记录了我想要的输出,但在HTML中,我的输出是[object Object]
答案 0 :(得分:0)
更改最后一行:
self.name = name
到
self.name = name.xxx[0].xxx