javascript - Using this instead of $scope inside controller - Stack Overflow

I am trying to follow style guide for angular and there wrote we should use this insted scope...Stylegu

I am trying to follow style guide for angular and there wrote we should use this insted scope...

Styleguide

Could someone explain me when I am able to use this?

Here is my try..... What I am doing wrong?

I am trying to toggle form.... here is my html code:

<a href="#" ng-click="formEdit(x)"  ng-if="!x.formEditShow">REPLY</a>
<a href="#" ng-click="formEdit(x)" ng-if="x.formEditShow" >CLOSE</a>

With classic $scope I would do like this inside my conroller :

$scope.formEdit = function(data){
data.formEditShow = !data.formEditShow;
}      

But with this it should look something like this(but don't work):

var vm = this;
vm.formEdit = formEdit;

function formEdit(data){
    data.formEditShow = !data.formEditShow;
}

Anyone can help me to understand this?

I am trying to follow style guide for angular and there wrote we should use this insted scope...

Styleguide

Could someone explain me when I am able to use this?

Here is my try..... What I am doing wrong?

I am trying to toggle form.... here is my html code:

<a href="#" ng-click="formEdit(x)"  ng-if="!x.formEditShow">REPLY</a>
<a href="#" ng-click="formEdit(x)" ng-if="x.formEditShow" >CLOSE</a>

With classic $scope I would do like this inside my conroller :

$scope.formEdit = function(data){
data.formEditShow = !data.formEditShow;
}      

But with this it should look something like this(but don't work):

var vm = this;
vm.formEdit = formEdit;

function formEdit(data){
    data.formEditShow = !data.formEditShow;
}

Anyone can help me to understand this?

Share Improve this question edited Sep 27, 2015 at 13:25 sdgluck 27.4k12 gold badges81 silver badges95 bronze badges asked Sep 27, 2015 at 12:33 Vladimir DjukicVladimir Djukic 2,0727 gold badges29 silver badges63 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

When you are using this(context) in controller instead of $scope, you must use controllerAs while defining html on page to access controller variables. Whenever you wanted to use variable bounded to this on view you could use alias of your controller. Below you can see vm is alias of controller.

ng-controller="myController as vm"

Then while accessing controller method an variable inside ng-controller div you need to use alias of your controller like ng-click="vm.formEdit(x)"

HTML

<a href="#" ng-click="vm.formEdit(x)"  ng-if="!x.formEditShow">REPLY</a>
<a href="#" ng-click="vm.formEdit(x)" ng-if="x.formEditShow" >CLOSE</a>

Assuming your controller is named FormController.

First step

The first step is to declare the route (or the ng-controller value if you are not using a router) as such:

FormController as form // name it semantically instead of a generic name

Due to the above configuration, angular will alias as form the instances of FormController.

HTML template

Then adapt your html template according to the alias you gave (form). I modified your html to keep only the essential part about the question. We are calling the functions form.reply and form.close.

<a href="#" ng-click="form.reply()">REPLY</a>
<a href="#" ng-click="form.close()">CLOSE</a>

Controller declaration

According to what we wrote above, our controller should look like that:

myApp.controller('FormController', function () {
    var vm = this;

    vm.reply = function () {
        // ...
    }

    vm.close = function () {
        // ...
    }
}

Notice the var vm = this; line? Theoretically we could get rid of this line, and store the functions reply and close in the this object. But depending of the context, this does not refer to the same object. In a callback function this would not refer to the controller but to the callback function. That's why we are caching the this that refers to the controller. We usually name this reference vm for viewmodel, as a controller controls a view.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745482634a4629628.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信