T
T
TekVanDo2014-04-02 09:51:10
JavaScript
TekVanDo, 2014-04-02 09:51:10

How to listen to model in angular using controller as syntax?

Don't know how to listen to controller property via $scope
codepen.io/Tek/pen/fGuwl - controller as syntax
http://codepen.io/Tek/pen/zqCpF /?editors=111 - $scope

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Zenkov, 2014-05-19
@TekVanDo

in connection with the study of angular, the same question arose, I myself will answer it
"as syntax" - in fact, just sugar for such an option

<div ng-controller="MyCtrl as my"> </div>

function MyCtrl($scope){
  $scope.my = this;
  this.val = "";
}

to verify this, you can output $scope to the console when the controller is launched and find the same my there as the scope property
. It follows that we need to observe not the variable, but the property of this very my
, that is, to observe this.val - you need to define such a watcher - listener
$scope.$watch('my.val', function(newVal){
   console.log(newVal);
});

Hope it helps someone figure it out

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question