我正在尝试创建一个简单的装载机。以下是我迄今所做的工作。谁能帮我看看,让我知道我哪里出了问题?
似乎没有添加CSS样式loading style-2。我的DOM刚刚显示:
<span class=""></span>我的指令:
angular.module('loaderModule', [
'restangular',
])
.controller('appLoaderController', ['$scope', function ($scope) {
$scope.$on('LOAD', function () {
$scope.loading = "loading style-2"
});
$scope.$on('UNLOAD', function () {
$scope.loading = ""
});
}])
.directive('spinLoader', function() {
return {
restrict: 'E',
transclude: true,
template: '<span class="{{ loading }}"></span><div ng-transclude ></div>'
};
});HTML:
<spin-loader>
<div ui-view></div>
</spin-loader>然后通过调用:$scope.$emit('LOAD')来使用它
发布于 2014-01-08 15:30:47
我会在您的指令中使用ng类,如下所示:
app.directive('spinLoader', function() {
return {
restrict: 'E',
transclude: true,
template: '<div ng-class="{loading: isLoading, style2: isLoading}" ng-transclude></div>'
};
});在控制器中,您可以将$scope.isLoading设置为true或false。
app.controller('Ctrl', function ($scope) {
var dummyLoadingVariable = false;
$scope.$on('LOAD', function () {
$scope.isLoading = true;
});
$scope.$on('UNLOAD', function () {
$scope.isLoading = false;
});
$scope.toggleLoading = function(){
dummyLoadingVariable = !dummyLoadingVariable;
if(dummyLoadingVariable){
$scope.$emit('LOAD');
} else {
$scope.$emit('UNLOAD')
}
}
});以及测试它的HTML:
isLoading: {{ isLoading }}
<br/>
<spin-loader>
<div ui-view>Transcluded</div>
</spin-loader>
<br/>
<button ng-click="toggleLoading()">toggleLoading()</button>下面是一个正在运行的Plunk:http://plnkr.co/edit/SetecF03aY6TnQilryWt?p=preview
https://stackoverflow.com/questions/20740213
复制相似问题