1

I am using the controllerAs syntax.

I used $controller to inherit from a parent (more like a base or abstract) controller. I found this question not long ago which I based on.

I noticed that when I use a function which uses a controller property (this.propName), it does not use the current controller this, but the parent's. Here's a demo (plunkr).

Here's a gist to both my parent controller and child controller.

0

2 Answers 2

1

Update sayMyName method to following:

function sayMyName() {
  alert(this.me);
}

As you are trying to pick me property on the base controller the alert should pick me value from the corresponding instance and not the instance when it was created which is vm

Updated plunker link

Sign up to request clarification or add additional context in comments.

1 Comment

Good catch, me is a local variable stored in closure.
1
var app = angular.module('myApp', [])
app.controller('BaseController',function() {
    this.me = 'Base';
    this.sayMe= function() {
        alert(this.me);
    }
});
app.controller('ChildController', function($scope, $controller) {
    var controller = $controller('BaseController as base', {$scope: $scope});
    angular.extend(this, controller);
    this.me = 'Child';
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.