I am trying to make a generic component to display variables and maybe call methods from its scope.
It felt quite natural to write <component config-name></component>, so I got stuck with the idea of filling the generic component's scope with variables from an attribute directive.
This directive would provide variables to display, methods to call, etc.
I ended up with something like this:
var app = angular.module('app', []);
app.component('component', {
template: '<li>{{data}}</li>'
});
app.directive('dirA', function() {
return {
restrict: 'A',
controller: function($scope) {
$scope.data = 'First directive!';
}
};
});
app.directive('dirB', function() {
return {
restrict: 'A',
controller: function($scope) {
$scope.data = 'Second directive!';
}
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.5/angular.min.js"></script>
<h2>Expectations</h2>
<ul>
<li>First directive!</li>
<li>Second directive!</li>
</ul>
<h2>Reality</h2>
<ul ng-app="app">
<component dir-a></component>
<component dir-b></component>
</ul>
However, the component's scope stays empty. How could I make the directives fill the scope of their component element?
Is this even the AngularJS way of doing things? If not, what would be the best way of doing something similar?
componentdirective and just usedirAanddirBas element directives?<comp-a dir-a></comp-a><comp-a dir-b></comp-a><comp-b dir-a></comp-b>ng-controller?<li>instead of a<button>and a popup before calling the method. Then I would have to duplicate all of the directives just to change their templateUrl, no?