angularjs - how to decorate input field with css class on eval error -


i want have simple input field can type in javascript expression, , have eval-ed on entry, kind of poor-man's calculator.

so far, have sort of works.. want decorate css classes when expression invalid. can suggest way it?

<head>     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>     <script>         function ctl($scope) {           $scope.expr = '1+1';         }          angular.module('filters', []).           filter('evaluate', function() {             return function(input) {               try {                 return eval(input);               } catch (e) {                 return input;               }             };           });          angular.module('main', ['filters']);</script> <head>  <body ng-controller="ctl" ng-app="main">   <div><input type="text" ng-model="expr"></div>   <span>{{expr|evaluate}}</span> </body> 

using suggestion offered manny d, modified code follows, , seems well. there caution in how pass param filter function in angular js not using 'this' param directly, think i'm ok here, doing that.. care comment?

<head>     <style type="text/css">     .red  {background-color: #fee;}     </style>      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>     <script>         function ctl($scope) {           $scope.cls = 'green';           $scope.expr = '1+1';         }          angular.module('filters', []).           filter('evaluate', function() {             return function(input, a1, a2) {               try {                 this.cls = 'green';                 return res = eval(input);               } catch (e) {                 this.cls = 'red';                 return input;               }             };           });          angular.module('main', ['filters']); // declare dependencies</script> <head>  <body ng-controller="ctl" ng-app="main">   <div><input type="text" ng-class="cls" ng-model="expr"></div>   <span>{{expr|evaluate}}</span> </body> 

the problem "this" won't work if you're doing more 1 filter evaluation. scope isn't isolated item you're working on. there easier way. use ng-class check whether value same filtered value , set class based on that. confirmed work multiple expressions.

http://plnkr.co/edit/crvscy?p=preview


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -