AngularJS 1.5 component example

MBX picture MBX · Dec 23, 2015 · Viewed 12.2k times · Source

I'm working on an Angularjs project using .component() with template property, but I don't know how to use templateUrl. Is there anyone familiar with that can provides me a working example? Thanks.

Answer

Dor Cohen picture Dor Cohen · Mar 8, 2016

To use the angular component properly I recommend using controllerAs syntax.

angular.module('myApp')
    .component('groupComponent', {
        templateUrl: 'app/components/group.html',
        controller: function GroupController(){
              this.innerProp = "inner";
        },
        controllerAs: 'GroupCtrl',
        bindings: {
            input: '<'
        }
    });

And on group.html you can consume at the following way:

<div>
{{GroupCtrl.input}}
{{GroupCtrl.inner}}
</div>

From the parent control You can pass any parameter as binding to the component, in this case from the parent HTML:

<group-component input="someModel">
</group-component>