這裏我試圖加載一個微調,而一個獲取請求訪問數據,然後顯示消息'獲取請求完成'一旦請求完成。如何在指令和查看頁面之間共享一個範圍變量?
這是我正在試圖切換顯示:
<span ng-show="!isFinishedLoading">
<img src="http://www.broadwaybalancesamerica.com/images/ajax-loader.gif">
</span>
<span ng-show="isFinishedLoading">
Get Request completed
</span>
但變量isFinishedLoading
不在範圍之內。
如何根據作用域變量切換微調器和請求之間的關係?
plunkr:https://plnkr.co/edit/F0XsOPZKq5HArFo9vtFs?p=preview
源:
2. http-hello2.html
<!doctype html>
<html ng-app="app">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<div ng-controller="FetchCtrl">
<label>Filter: <input ng-model="search"></label>
<div ng-repeat="sourceUrl in sourceUrls | filter:search track by $index ">
<status-viewer url="sourceUrl"> </status-viewer>
<span ng-show="!isFinishedLoading">
<img src="http://www.broadwaybalancesamerica.com/images/ajax-loader.gif">
</span>
<span ng-show="isFinishedLoading">
Get Request completed
</span>
</div>
</div>
</body>
</html>
<!--<h1>{{url}}</h1>-->
<div>
<p>{{model}}</p>
</div>
var myapp = angular.module('app', []).controller('FetchCtrl', FetchCtrl)
myapp.directive('statusViewer', function ($http , $interval) {
return {
restrict: 'E',
templateUrl: 'mytemplate.html',
scope: {
url: '='
},
link: function (scope, elem, attrs, ctrl) {
console.log('invoked')
scope.isFinishedLoading = false;
$http.get(scope.url).success(function (data) {
scope.model = data;
scope.isFinishedLoading = true;
});
}
};
});
function FetchCtrl($scope, $http, $q , $parse) {
$scope.sourceUrls = [
'http-hello2.html'
,'http-hello2.html'];
}