Defining your component using bindings not a direct equivalent to defining your directive using scope , although both are defined for using controllerAs . This is because your component will be bound directly to the controller, and your directive will be bound to $scope (by default).
I used your code in the snippet below, slightly modified to allow the component and directives to be used together. I also added an extra directive that uses bindToController:true to demonstrate a directive that behaves a bit more like a component that binds its value attribute directly to the controller, and not to $scope .
I also used a very simple generic template that tries to show the values ββof the attached attributes, looking for them on $scope and then looking for them on vm (ControllerAs).
(function() { "use strict"; var templateBody = '<h2>$scope</h2>' + '<p>value: {{value}}</p><p>dimension: {{dimension}}</p>' + '<h2>vm</h2>' + '<p>vm.value: {{vm.value}}</p><p>vm.dimension: {{vm.dimension}}</p>'; angular .module('xyz', []) .component('triangularStatusComponent', { bindings: { value: '=', dimension: '=?' }, template: '<div><h1>Triangular Status Component</h1>' + templateBody + '</div>', controller: TriangularStatusController, controllerAs: 'vm' }) .directive('triangularStatusDirective', triangularStatusDirective) .directive('triangularStatusDirectiveBound', triangularStatusDirectiveBound); function triangularStatusDirective() { var directive = { scope: { value: '=', dimension: '=?' }, replace: true, template: '<div><h1>Triangular Status Directive</h1>' + templateBody + '</div>', controller: TriangularStatusController, controllerAs: 'vm', }; return directive; } function triangularStatusDirectiveBound() {
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script> <div ng-app="xyz"> <triangular-status-component value="'componentValue'" dimension="'componentDimension'"> </triangular-status-component> <hr> <triangular-status-directive value="'directiveValue'" dimension="'directiveDimension'"> </triangular-status-directive> <hr> <triangular-status-directive-bound value="'directiveValueBound'" dimension="'directiveDimensionBound'"> </triangular-status-directive-bound> </div>
If you find that your code works as a directive where your values ββare tied to $scope , but not as a component where your values ββare tied to a controller, I would suggest that your html template (most likely?), Or your controller function relies trying to access your values ββas if they were on $scope . To confirm this, you may notice that there will be errors on your javascript server that will help you from scratch.
source share