Angular Directive to component angular 1.5

mtkilic

I have this directive, that i would like to make a component

angular.module('app')
.directive('year', function () {
    var controller = ['$scope', function ($scope) {
        $scope.setYear = function (val) {
            $scope.selectedyear = val;
        }
    }];
    return {
        restrict: 'E',
        controller: controller,
        templateUrl: "views/year.html"
    };
});

This is what I got so far:

 angular.module('app') 
.component('year', {
        restrict: 'E',
        controller: controller,
        templateUrl: "views/year.html"
    });

I am not sure how to move my var controller into .component

Poyraz Yilmaz

There are few things you should do convert your directive to component

  • There is no restrict property for component as it is restricted to elements only.
  • For controller you could just set as you did at directive declaration but outside of it.
  • Controllers for components use controllerAs syntax as default so get rid of $scope

So your component should look like this...

angular.module('app') 
    .component('year', {
        controller: ComponentController,
        templateUrl: "views/year.html"
    });

function ComponentController(){
    var $ctrl = this;

    $ctrl.setYear = function (val) {
       $ctrl.selectedyear = val;
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Angular 5 Updating Directive from Component

Angular 1 component vs directive definition

@Directive vs @Component in Angular

Angular component (with a directive) testing

angular 5 - "this" refers to Directive instead of Component, when passing a function from component to directive through @Input

Angular 5 Structural Directive

Angular 1 directive component has different behavior in modal and out of the modal

Angular 9 : Use component as directive

Angular 2 component directive not working

adding directive to a angular 2 component

Angular 5 - lifecycle hook in directive

Angular directive for CKEditor 5 not working

Angular 5 -> providers in Component

Angular 1 directive inside angular 2 application

Angular Directive to add class to component on scroll?

Angular testing component with mocked structural directive fails

Use Angular 6 Directive to set property on a component

Passing param using directive to child component angular

Directive to Component Angular 1.5x

renderComponent in Angular ivy with extra component/directive declarations

Angular directive not working in dynamically loaded component

Call a Component method in Directive Angular 2

$postLink of an angular component/directive running too early

Apply attribute directive on component in Angular 4

Angular 4 call directive method from component

Limit directive to specific host (component) in Angular

angular editable control multi language component or directive

How to add a component using directive in angular

What is the difference between component and directive in Angular 2?