-1
我無法獲得控制器和服務之間的數據綁定工作。 我有一個控制器和一個進行HTTP調用的工廠。我希望能夠從其他服務調用工廠方法並查看控制器屬性得到更新。我嘗試了不同的選擇,但他們似乎都沒有工作。任何想法將不勝感激。控制器和服務之間的Angularjs數據綁定
請在這裏看到的代碼:
http://plnkr.co/edit/d3c16z?p=preview
下面是JavaScript代碼。
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
});
app.controller('EventDetailCtrl', ['$http', 'EventDetailSvc', '$scope',
function ($http, EventDetailSvc, $scope) {
this.event = EventDetailSvc.event;
EventDetailSvc.getEvent();
console.log(self.event);
$scope.$watch(angular.bind(this, function() {
console.log('under watch');
console.log(this.event);
return this.event;
}), function (newVal, oldVal) {
console.log('under watch2');
console.log(newVal);
this.event = newVal;
});
}])
.factory('EventDetailSvc', ['$http', function ($http) {
var event = {};
var factory = {};
factory.getEvent = function() {
$http.get('http://ip.jsontest.com')
.then(function (response) {
this.event = response.data;
console.log('http successful');
console.log(this.event);
return this.event;
}, function (errResponse) {
console.error("error while retrieving event");
})
};
factory.event = event;
return factory;
}]);