【发布时间】:2016-07-28 18:56:06
【问题描述】:
我想在我的自定义 AngularJS 指令中使用 $timeout,但它不起作用。我的最后一个实现如下所示:
var App = angular.module('App', []);
App.controller('Controller', function($scope){
$scope.test = true;
$scope.toggle = function(){ $scope.test = !$scope.test;};
});
App.directive('showTemporary', ['$timeout', function ($timeout) {
return {
restrict: 'A',
link: function(scope, element, attr){
scope.$watch(attr.showTemporary, function(value){
element.css('display', value ? '' : 'none');
});
$timeout(element.css('display', 'none'), attr.hideDelay);
}
}
}]);
和标记:
<div ng-app='App'>
<div ng-controller='Controller'>
<button ng-click='toggle()'>toggle</button>
<div show-temporary='test' hide-delay="5000"> visible</div>
</div>
</div>
【问题讨论】:
标签: javascript angularjs angular-directive