我想在执行 $http 请求时制作一个显示微调器的指令。
我认为这会起作用(例如加载用户):
主控制器
app.controller('UserListController', function($scope, $http) {
$scope.users = [];
$scope.loading = true;
$http.get('/users').then(function(response) {
$scope.users = response.data;
}).finally(function() { $scope.loading = false; });
});
使用指令的模板
<div request-loading="loading">
<ul>
<li ng-repeat="user in users">{{ user.name }}</li>
</ul>
</div>
请求加载directive.js
app.directive('requestLoading', function() {
return {
scope: {
requestLoading: '='
},
transclude: true, // I think this is not Ok
templateUrl: 'request-loading-directive.html'
};
});
请求加载模板.html
<div ng-show="requestLoading" class="super-cool-spinner">
Loading text with the spinner...
</div>
// This is what is not working
// I thought the content of the users (ul and li) would be render in this div
// when the request is Ok.
<div ng-transclude ng-show="! requestLoading"></div>
你能帮忙的话,我会很高兴!谢谢。