【发布时间】:2014-11-27 07:34:41
【问题描述】:
我有一项服务,我只返回一个包含所有可用视频的列表,然后将其注入控制器中,因此我可以通过ng-repeat 列出这些视频。就这样
videosApp.factory('VideosService', function ($http, $rootScope) {
$rootScope.videos = {
list: [],
};
$http.get('/panel/videos')
.then(
function (response) {
$rootScope.videos.list = response.data;
},
function () {
alert('what is the velocity of a swallow?');
}
);
return $rootScope;
});
videosApp.controller('VideosController', [
'$scope', '$http', '$filter', '$window',
function ($scope, $http, VideosService) {
$scope.deleteVideo = function(id) {
if (confirm('Are you sure you want to delete this video?')) {
$http.delete('/panel/videos/'+id)
.then(
function (response) {
growl(response.data.message, 'success');
},
function (response) {
growl(response.data.message, 'danger');
}
);
}
}
}
]);
现在,对于 HTML 输出:
<div id="videos-listing" class="row" ng-controller="VideosController">
<ul>
<li ng-repeat="video in videos.list">
{{video.name}}
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
<li role="presentation">
<a role="menuitem" href="#delete" ng-click="deleteVideo(video.id)">Delete</a>
</li>
</ul>
</li>
</ul>
</div>
现在,如您所见,还有一个 Delete 函数,它可以完美运行。我的问题在于如何在删除特定项目后刷新该列表。
我 cab 将 get /panel/videos 包装在一个函数中,并在删除成功后再次调用它,但是,如何刷新该 ng-repeat?或者,有没有更好的方法?
【问题讨论】:
-
你有没有试过在删除后调用/panel/videos,那应该会自动刷新视图!
标签: javascript angularjs angularjs-ng-repeat reload