javascript - How to use $watch within directive to update class (no jQuery)? -


i'm trying create app poll live data every 10 seconds or so. , if data changes should highlighted in view. have managed this, little jquery. want template in angular instead.

this controller:

angular.module('myapp.controllers', []) .controller('mainctrl', ['$rootscope', 'livedatapoller', function($rootscope, livedatapoller) {     $rootscope.livedata = livedatapoller.getdata(); }]); 

this directive:

angular.module('myapp.directives', []). directive('livevalue', function() {     return {         restrict: 'a',         scope: {             val: '='         },         link: function(scope, element, attrs) {             scope.$watch('val', function(newvalue, oldvalue) {                 if(newvalue && oldvalue && (newvalue !== oldvalue)) {                     if(newvalue < oldvalue) {                         element.removeclass("up").addclass("down");                     } else {                         element.removeclass("down").addclass("up");                     }                 } else {                     element.removeclass("down").removeclass("up");                 }             });         }     } }); 

this view:

<div live-value val="livedata.randominteger">{{livedata.randominteger}}</div> 

is possible have add/remove class changed use transclude , template instead? don't want mix jquery in this.

just let me know if unclear.

sample demo: http://plnkr.co/edit/uxhocexywllkmrkzxx1h?p=preview

a scope variable can used track new changes , use 2 way binding update class in html.

app.directive('livevalue', function() {   return {     restrict: 'e',     replace: true,     scope: {       data: '=', change:'='     },     template: '<div class="{{change}}">the value({{data}}) has gone: {{change}}</div>',     link: function(scope, element, attrs) {       scope.change = '';       scope.$watch('data', function (newvalue, oldvalue) {         if (newvalue == oldvalue) {           scope.change='nochange';         }else if(newvalue<oldvalue){           scope.change='down';         }else{           scope.change='up';         }       });     }   } }); 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -