Reputation: 11
The directive bellow return an object "directive". Can I return it to a function?
var sharedApp = angular.module("sharedApp", []);
sharedApp.directive("showAvatar", function () {
var directive = {
restrict: "ACEM",
replace: true,
template: "<h2>This is directive template of shared module</h2>"
};
return directive;
});
(Updated)For example
var sharedApp = angular.module("sharedApp", []);
sharedApp.directive("showAvatar", function () {
function directive(){
this.restrict = "ACEM";
this.replace = true;
this.template = "<h2>This is directive template of shared module</h2>";
}
return directive;
});
Upvotes: 1
Views: 1351
Reputation: 1025
Return it to a function means do you want to return directive object from your directive to JS controller..
Define your Directive like this
App.directive('showAvatar', function() {
return {
restrict : 'EA',
scope : {
returnFunction : '&'
},
controller : function($scope) {
$scope.directive = {
restrict: "ACEM",
replace: true,
template: "<h2>This is directive template of shared module</h2>"
};
$scope.returnFunction({data : $scope.directive});
}
});
Write your directive html tag like this
<show-avathar return-function = "getDataFromDirective(data)">
And finally in your controller, declare the function getDataFromDirective()
$scope.getDataFromDirective = function(data){
alert(data);
}
Upvotes: 2