Home > Back-end >  angularjs: controllerAs vs $ctrl defined
angularjs: controllerAs vs $ctrl defined

Time:06-13

I have code to create a popup,like this:

var modalPromise = $uibModal.open({
                        templateUrl: .....,
                        controllerAs: '$ctrl',
                        size: 'lg',
                        controller: function createController($uibModalInstance, items) {
                            var itemsSelected = items.itemSelected;
                            return $controller('ItemUpdateCtrl', {
                                $uibModalInstance: $uibModalInstance,
                                $http: $http,
                                ......
                            })
                        }
                    });

So this launches a popup, associating template with controller ItemUpdateCtrl.

In ItemUpdateCtrl, I have:

var $ctrl = this;

What is the role of controllerAs in first piece of code?Is there any connection between $ctrl assigned to controllerAs , and $ctrl defined in ItemUpdateCtrl.

CodePudding user response:

controllerAs will allow you to access your controller within your template!

For instance, if you have:

$uibModal.open({
  templateUrl: './template.html',
  controllerAs: '$ctrl',
  controller: function () {
    this.property = 'Santiago';
  }
});

... then, in template.html:

<h1>Controller property: {{ $ctrl.property }}</h1>

... will compile to:

<h1>Controller property: Santiago</h1>
  • Related