3

Can someone please help!

I am very much confused about this and $scope in angularJS.

Suppose here is my html:

<div ng-controller = "myController"> <input type = "text" data-ng-model = "testing"> </div> 

My controller

When I write:

$scope.testing = "Rest"; // this works fine 

but:

this.testing = "Rest" // this does not work 

This works when I give alias to controller and use it

<div ng-controller = "myController as ctrl"> <input type = "text" data-ng-model = "ctrl.testing"> </div> 

Also here is my complete code where I am totally confused:

<div ng-controller = "myController as ctrl"> <input type = "text" data-ng-model = "ctrl.testing"> </div> <ng-form> {{testing}} <input type="button" ng-click="test()"> </ng-form> 

Now in my controller

$scope.test = function(){ this.testing = "Work" // This thing changes only the form variable not the actual why ?? } 

And also ng-form does not create its own scope.. then

Also if in controller if I defined my test function

this.test = function() { }// This is not called why?? 

Can someone please explain what's going on here. I am very much confused about this and scope.

Also $apply - does it also check my variables? Also this is not a duplicate i have many aspects which i am very much confused.

4
  • 3
    I was reading about the same here. That doesn't helped me. So, I am not voting to close this post. Commented Dec 14, 2016 at 9:49
  • Might help - 'this' vs $scope in AngularJS controllers Commented Dec 14, 2016 at 9:52
  • this.testing = "Work" isn't changing a ng-model value on view because $scope.test function has different this than controller this. do place this inside controller like var self = this; & then do self.testing = "work". For 2nd question why this.test function isn't getting called? you have to change your ng-click to ng-click="ctrl.test()" to make it working Commented Dec 14, 2016 at 9:53
  • Ya but my form does not create any scope so i need to write it ctrl.test? Commented Dec 14, 2016 at 10:17

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.