基于此示例throbberDirective,如何将自定义指令注入控制器,以便可以调用函数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()
});
}]);
答案 0 :(得分:3)
以下是使用该示例调度事件以显示/隐藏微调器的示例:
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');
});
}]);