$compileProvider.component()

component(name, options);

Register a component definition with the compiler. This is a shorthand for registering a special type of directive, which represents a self-contained UI component in your application. Such components are always isolated (i.e. scope: {}) and are always restricted to elements (i.e. restrict: 'E').

Component definitions are very simple and do not require as much configuration as defining general directives. Component definitions usually consist only of a template and a controller backing it.

In order to make the definition easier, components enforce best practices like use of controllerAs, bindToController. They always have isolate scope and are restricted to elements.

Here are a few examples of how you would usually define components:

var myMod = angular.module(...);
myMod.component('myComp', {
  template: '<div>My name is {{$ctrl.name}}</div>',
  controller: function() {
    this.name = 'shahar';
  }
});

myMod.component('myComp', {
  template: '<div>My name is {{$ctrl.name}}</div>',
  bindings: {name: '@'}
});

myMod.component('myComp', {
  templateUrl: 'views/my-comp.html',
  controller: 'MyCtrl',
  controllerAs: 'ctrl',
  bindings: {name: '@'}
});

For more examples, and an in-depth guide, see the component guide.


See also $compileProvider.directive().

Parameters

Param Type Details
name string

Name of the component in camelCase (i.e. myComp which will match <my-comp>)

options Object

Component definition object (a simplified directive definition object), with the following properties (all optional):

  • controller â {(string|function()=} â controller constructor function that should be associated with newly created scope or the name of a registered controller if passed as a string. An empty noop function by default.
  • controllerAs â {string=} â identifier name for to reference the controller in the component's scope. If present, the controller will be published to scope under the controllerAs name. If not present, this will default to be $ctrl.
  • template â {string=|function()=} â html template as a string or a function that returns an html template as a string which should be used as the contents of this component. Empty string by default.

    If template is a function, then it is injected with the following locals:

    • $element - Current element
    • $attrs - Current attributes object for the element
  • templateUrl â {string=|function()=} â path or function that returns a path to an html template that should be used as the contents of this component.

    If templateUrl is a function, then it is injected with the following locals:

    • $element - Current element
    • $attrs - Current attributes object for the element
  • bindings â {object=} â defines bindings between DOM attributes and component properties. Component properties are always bound to the component controller and not to the scope. See bindToController.

  • transclude â {boolean=} â whether content transclusion is enabled. Disabled by default.
  • $... â additional properties to attach to the directive factory function and the controller constructor function. (This is used by the component router to annotate)

Returns

ng.$compileProvider

the compile provider itself, for chaining of function calls.

doc_AngularJS
2016-03-29 16:10:16
Comments
Leave a Comment

Please login to continue.