Skip to content

Instantly share code, notes, and snippets.

@vinay13
Last active January 12, 2016 05:23
Show Gist options
  • Save vinay13/536b758b5e9861dc3bba to your computer and use it in GitHub Desktop.
Save vinay13/536b758b5e9861dc3bba to your computer and use it in GitHub Desktop.
//important links
http://www.bennadel.com/blog/2852-understanding-how-to-use-scope-watch-with-controller-as-in-angularjs.htm
http://fdietz.github.io/recipes-with-angular-js/controllers/responding-to-scope-changes.html
https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$watch
$watch
//this is the value we are watching
vm.fooCount =0;
//When u watch a "vm.*" value from within the controller, you are making the assumption(ie, creating coupling) that your View is using the same variable - "vm" - externally that you are using intenally.
//Caution: In our case, this is not true - the view is using the
"appController" alias
$scope.watch(
"vm.fooCount",
function handleFooChange(newValue,oldValue){
console.log("vm.fooCount: ", newvalue);
});
//if your controller and your view are not using the same view-model
//alias ,then you can explicitly watch the value that our view is using.
//Here we are wacyhing "appController" instead of "vm". This might make you dry-heave a tiny bit.
but this is essential what you are doing when you are "vm.*" as well.
$scope.watch(
"app.controller.fooCount",
function handleFooChange(newValue,oldValue){
console.log("app.controller.fooCount:",newValue);
}) ;
//Notice that "vm.fooCount" never watched the value we were incrementing.
This is because watching "vm.*" only works when both the controller and the view follow
$watch
//this is the value we are watching
vm.fooCount =0;
//When u watch a "vm.*" value from within the controller, you are making the assumption(ie, creating coupling) that your View is using the same variable - "vm" - externally that you are using intenally.
//Caution: In our case, this is not true - the view is using the
"appController" alias
$scope.watch(
"vm.fooCount",
function handleFooChange(newValue,oldValue){
console.log("vm.fooCount: ", newvalue);
});
//if your controller and your view are not using the same view-model
//alias ,then you can explicitly watch the value that our view is using.
//Here we are wacyhing "appController" instead of "vm". This might make you dry-heave a tiny bit.
but this is essential what you are doing when you are "vm.*" as well.
$scope.watch(
"app.controller.fooCount",
function handleFooChange(newValue,oldValue){
console.log("app.controller.fooCount:",newValue);
}) ;
//Notice that "vm.fooCount" never watched the value we were incrementing.
This is because watching "vm.*" only works when both the controller and the view follow
$watch
//this is the value we are watching
vm.fooCount =0;
//When u watch a "vm.*" value from within the controller, you are making the assumption(ie, creating coupling) that your View is using the same variable - "vm" - externally that you are using intenally.
//Caution: In our case, this is not true - the view is using the
"appController" alias
$scope.watch(
"vm.fooCount",
function handleFooChange(newValue,oldValue){
console.log("vm.fooCount: ", newvalue);
});
//if your controller and your view are not using the same view-model
//alias ,then you can explicitly watch the value that our view is using.
//Here we are wacyhing "appController" instead of "vm". This might make you dry-heave a tiny bit.
but this is essential what you are doing when you are "vm.*" as well.
$scope.watch(
"app.controller.fooCount",
function handleFooChange(newValue,oldValue){
console.log("app.controller.fooCount:",newValue);
}) ;
//Notice that "vm.fooCount" never watched the value we were incrementing.
This is because watching "vm.*" only works when both the controller and the view follow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment