0

I want to have a simple input field where you can type in a javascript expression, and have it be eval-ed on entry, kind of like a poor-man's calculator.

So far, what I have sort of works.. but now I want to decorate it with css classes when the expression itself is invalid. Can someone suggest a way to do 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 the suggestion offered by Manny D, I modified the code to look as follows, and all seems well. There is a caution in how to pass param to a filter function in angular js about not using the 'this' param directly, but I think I'm ok here, doing that.. Anyone care to 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>
1
  • 1
    Have you tried using ngClass? Commented Jul 18, 2013 at 16:46

1 Answer 1

0

The problem with the "this" is that it won't work if you're doing more than one filter evaluation. The scope isn't isolated to the item you're working on. But there is an easier way. Just use ng-class to check whether the value is the same as the filtered value and set the class based on that. I also confirmed that it would work with multiple expressions.

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.