JavaScript | Angular | Controller As Syntax: Cannot Use `this`

后端 未结 2 773
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-20 03:55

Cannot Use ControllerAs this

Can anyone explain the following scenario to me, please?

Works

ng-controller=\"Parent as thus\"         


        
2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-20 04:48

    You are cannot use this as the instance name. this is a reserved keyword.

    It would be the equivalent of doing:

    var this = new Controller();
    

    You can see that is wrong. It should be something like:

    var ctrl = new Controller();
    

    This is essentially what angularJs does behind the scenes when you do controllerAs="Controller as ctrl"

提交回复
热议问题