Lets say I have the following HTML
structure:
<div ng-app="testApp">
<div ng-controller="controller1">
{{controller1}}
</div>
<div ng-controller="controller2">
{{controller2}}
</div>
<div ng-controller="controller3">
{{controller3}}
<test-directive></test-directive>
</div>
</div>
Inside ng-controller="controller3"
there is a custom directive.
The directive is as followed:
ang.directive("testDirective", function() {
return {
restrict: 'AE',
link: function (scope, elem, attrs) {
console.log(scope);
},
template: "<h3>I return $scope.controller3</h3>"
}
});
The $scope
now holds the data from ng-controller="controller3"
.
How can i link a custom controller to it? So instead the directive passes its parent controller, I want too pass for example controller1 its data.
I have to be able to pass any controller to it, because the directive is depended on the data inside a given controller.
I cannot replace the <test-directive>
inside the required controller
Here a fiddle you can play with
var ang = angular.module('testApp', []);
ang.controller("controller1", controller1);
function controller1($scope) {
$scope.controller1 = "controller1";
}
ang.controller("controller2", controller2);
function controller2($scope) {
$scope.controller2 = "controller2";
}
ang.controller("controller3", controller3);
function controller3($scope) {
$scope.controller3 = "controller3";
}
ang.directive("testDirective", function() {
return {
restrict: 'AE',
link: function (scope, elem, attrs) {
console.log(scope);
},
template: "<h3>I return $scope.controller3</h3>"
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.min.js"></script>
<div ng-app="testApp">
<div ng-controller="controller1">
{{controller1}}
</div>
<div ng-controller="controller2">
{{controller2}}
</div>
<div ng-controller="controller3">
{{controller3}}
<test-directive></test-directive>
</div>
</div>
Real case scenario:
I made a pagination directive. You can call the directive by placing <ng-pagination></ng-pagination>
anywhere on the page.
The directive creates a pagination from a given dataset. The dataset however can hold anything. It doens't depend on values or whatever.
There is only one requirement for the directive. It looks inside the controller if it has a $scope
called $scope.result
. The $scope.result
gets filled with data from an API call.
The pagination then recreates a object
with 10 results per page.
The problem I have is: The ng-controller
can be anywhere on the page also can the <ng-pagination></ng-pagination>
.
What i want to archieve is something like this:
I assign an data attribute to the [ng-pagination]
. Called: data-controller
. You can then pass the controller name to it. The directive then has access to all the data inside that controller.
It would then look like this:
<ng-pagination data-controller="controller1" show-next="true" ...></ng-pagination>
UPDATE
I found out that you can assign a controller by doing:
ang.directive("directive", function() {
return {
controller: "[CONTROLLER NAME]"
}
});
I though, perhaps this is possible:
ang.directive("directive", function() {
return {
controller: "{{controller}}",
link: function (scope, elem, attrs) {
$scope.controller = "[CONTROLLER NAME]"
}
}
});
However, it gives me the error:
Error: ng:areq Bad Argument
Argument '{{controller}}' is not a function, got undefined