如何在控制器中加载指令
How to load directive in controller?
基于这个例子悸动指令,如何将自定义指令注入控制器,以便调用函数show()hide()?我无法摆脱以下错误:
Error: [$injector:unpr] Unknown provider: inputThrobberProvider <- inputThrobber <- landingCtrl
示例代码:
var app = angular.module('myapp', ['ngAnimate', 'ui.router', 'templates']);
app.directive('inputThrobber', [function() {
return {
restrict: 'A',
require: 'ngModel',
controller: function($scope, $element) {
var inputThrobberCtrl = {
show: function() {
$element.addClass('throbbing');
},
hide: function() {
$element.removeClass('throbbing');
}
};
return inputThrobberCtrl;
}
};
}])
.controller('landingCtrl', ['$scope', 'geolocation', 'inputThrobber', function($scope, geolocation, inputThrobber) {
// inputThrobber.show()
geolocation.getAddress().then(function(address) {
$scope.address = address;
}).catch(function(err) {
$scope.error = error;
$scope.address = '';
})
.finally(function() {
// $inputThrobber.hide()
});
}]);
下面是一个使用调度事件以显示/隐藏微调器的示例:
app.directive('inputThrobber', [function() {
return {
restrict: 'A',
require: 'ngModel',
controller: function($scope, $element) {
$scope.$on('startThrobbing', function() {
$element.addClass('throbbing');
});
$scope.$on('stopThrobbing', function() {
$element.removeClass('throbbing');
});
}
};
}])
.controller('landingCtrl', ['$scope', 'geolocation', function($scope, geolocation) {
$scope.$broadcast('startThrobbing');
geolocation.getAddress().then(function(address) {
$scope.address = address;
}).catch(function(err) {
$scope.error = error;
$scope.address = '';
})
.finally(function() {
$scope.$broadcast('stopThrobbing');
});
}]);
相关文章:
- 指令加载真的很长,检查加载时间的方法
- 在Angular JS中POST后重新加载指令方法
- 如何在控制器中加载指令
- 延迟加载角度应用程序不会加载指令
- 在加载指令模板之前加载指令 ng 包含内容
- ng-repeat中的加载指令(动态模板URL)
- ng-include 忽略加载指令
- 加载指令后运行函数
- 加载指令模板后执行 Javascript
- Meteor AngularJS应用程序未加载指令
- 不要为某些路由加载指令
- 基于值加载指令
- AngularJS在使用服务时未正确加载指令
- 加载指令时调用init函数
- 如何实现AngularJS的加载指令
- 在加载文件时显示一个加载指令
- 在加载指令模板之前没有初始化指令的作用域,导致无法加载谷歌地图
- 在控制器事件上重新加载指令
- 如何管理AngularJS中加载指令模板的404错误
- 当惰性加载指令时,它'We’我们永远不要跑步