Skip to content

How to take actions when data changes

Julian Knight edited this page Feb 10, 2017 · 2 revisions

Dealing with not having access to Angular's $scope object (using $watch)

<div ng-bind-html="msg.payload"></div>
<script>
//console.dir(scope) // this also works
//console.dir(scope.msg) // This doesn't because scope.msg doesn't yet exist

// Lambda function to access the Angular Scope
;(function(scope) {
    //console.log('--- SCOPE ---')
    //console.dir(scope) // this works but you only get it once (on startup)
    //console.dir(scope.msg) // Doesn't work for  because scope.msg doesn't yet exist

    //Have to use $watch so we pick up new, incoming msg's
    scope.$watch('msg.payload', function(newVal, oldVal) {
        console.log('- Scope.msg -')
        console.dir(scope.msg)
    })
    
})(scope)
</script>