Last active
October 23, 2016 18:47
-
-
Save magarcia/384af019aef2ef465f3e08c5b8f905ec to your computer and use it in GitHub Desktop.
Blog: $watch in Angular2 (https://thingsofgeek.com/2016/07/04/watch-with-angular2.html)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, Input } from '@angular/core'; | |
@Component({ | |
selector: 'example-component', | |
}) | |
export class ExampleComponent { | |
public internalVal = null; | |
constructor() {} | |
@Input('externalVal') | |
set updateInternalVal(externalVal) { | |
this.internalVal = externalVal; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var module = angular.module("myApp"); | |
module.directive('exampleDirective', function () { | |
return { | |
template: '<div>{{internalVar}}</div>', | |
scope: { | |
externalVar: "=" | |
}, | |
controller: function(scope, element) { | |
scope.$watch('externalVar', function(newVal, oldVal) { | |
if (newVal !== oldVal) { | |
scope.internalVar = newVal; | |
} | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment