What exactly does a selector do in angular 2?

前端 未结 3 1570
悲哀的现实
悲哀的现实 2020-12-14 06:11

What does the selector do in this case?

import { Component } from \'@angular/core\';
import { HighlightDirective } from \'./highlight.directive\';

@Componen         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-14 06:24

    If we say in simple term selector is name which is used in our view like html tag. as we all know angular2 is component based. so selector is just provide the name of the component which is being called by its className in the directives list and called by selector name in the view of the another component like this:-

    //suppose this is our component

    @Component({
     selector : 'mycomponent'
     ....
    })
    export class Mycomponent{ }
    

    now we can use this component in another component like this -

    @Component({
     selector : 'secondcomponent',
     directives: [Mycomponent],  //here we use class name instead of selector name
     template: ''  //here we used selector name
     ....
    })
    export class Mycomponent{ }
    

    Also we can say selector is a complete functionality name used as html tag in the view.

提交回复
热议问题