Skip to content

Instantly share code, notes, and snippets.

@vinay13
Created January 12, 2016 04:55
Show Gist options
  • Save vinay13/dabfdbed9a3227beac2d to your computer and use it in GitHub Desktop.
Save vinay13/dabfdbed9a3227beac2d to your computer and use it in GitHub Desktop.
$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