Angular 1.5 component vs. old directive - where is a link function?

前端 未结 6 2017
夕颜
夕颜 2020-12-12 23:00

I\'ve been reading this nice recent article about new .component() helper in Angular 1.5, which is supposed to help everyone to migrate to Angular 2 eventually.

6条回答
  •  -上瘾入骨i
    2020-12-12 23:12

    Update (from august 22, 2017): $inject is recommended way for doing this in AngularJS. Read Styleguide: Styleguide link and AngularJS docs: AngularJS docs

    For using DOM bindings in components instead of creating directive with link function you can inject '$element' or other service you need in your controller function, e.g.

    app.component('pickerField', {
        controller: PickerField,
        template: 'Your template goes here'
      });
    
      PickerField.$inject = ['$element'];
    
      function PickerField(element) {
        var self = this;
        self.model = self.node.model;
        self.open = function() {
          console.log('smth happens here');
        };
        element.bind('click', function(e) {
          console.log('clicked from component', e);
          self.open();
        });
      }

提交回复
热议问题