AngularJS如何更改模型的一部分 [英] AngularJS how to change part of a model
问题描述
我有一个页面上改变模型的部分操作链接。这些链接有一个火一个返回被修改模型的一部分的服务器上的动作NG-点击功能。我的$ http.get(...)的结果分配给已更改模型的一部分。但是,数据从来不解决。
I have action links on a page that change parts of the model. The links have ng-click functions that fire a server action that returns the part of the model that was altered. I assign the result of $http.get(...) to the part of the model that was changed. But the data is never resolved.
GuideControllers.controller('VideoDetailCtrl', ['$scope', '$http', '$routeParams', 'Video',
function($scope, $http, $routeParams, Video, Preference) {
$scope.video = Video.get({ id: $routeParams.id });
$scope.addToWatchlist = function(id) {
$scope.video.prefs = $http.get('/api/preference/'+id+'/add_to_watchlist.json')
}
}
]);
第一Video.get(...)填写并承诺解决的时候,最终改变了页面,视频。preFS是正确的模型。但是,当我单独获取视频。preFS他们从来没有得到解决。我试图挽救在一个单独的变量$ http.get承诺然后在$ http.get(...)。成功(...我复制出来的变量中的部分视频。preFS,但没有无论是工作,因为临时可行的是一个承诺 - 我猜
The first Video.get(...) fills in the model with a promise that eventually changes the page, the video.prefs are correct when resolved. But when I fetch the video.prefs individually they never get resolved. I tried saving the $http.get promise in a separate variable then on $http.get(...).success(... I copied the parts out the the variable to videos.prefs but that didn't work either since the temp viable was a promise--I guess.
我应该如何通过询问服务器只是位改变模型的一部分?
How should I change part of the model by asking the server for just that bit?
推荐答案
这应该这样做:
GuideControllers.controller('VideoDetailCtrl', ['$scope', '$http', '$routeParams', 'Video',
function($scope, $http, $routeParams, Video, Preference) {
$scope.video = Video.get({ id: $routeParams.id });
$scope.addToWatchlist = function(id) {
$http.get('/api/preference/'+id+'/add_to_watchlist.json').success(function(data) {
$scope.video.prefs = data;
});
}
}
]);
这篇关于AngularJS如何更改模型的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!