New to Angular - Computed Variables

前端 未结 7 1642
一个人的身影
一个人的身影 2021-02-04 00:33

I am moving to Angular from Knockout, and I have a few issues. I\'m assuming that I must be doing something a non-angular type of way.

http://jsfiddle.net/LostInDaJungle

7条回答
  •  耶瑟儿~
    2021-02-04 01:06

    I'm new to AngularJS but I think that $parse could be used:

    http://docs.angularjs.org/api/ng/service/$parse

    This is interesting if you have the expression as a string. You can use a path of properties and that string can be generated dynamically. This works if you don't know the expression at compile time, a lot like eval() but probably a lot faster and maybe more secure(?).

    Here's an example:

    function Ctrl($scope,$parse) {
      var expression = 'model.val1 + model.val2';//could be dynamically created
      $scope.model = {
        val1: 0,
        val2: 0,
        total: function() { 
            return ($parse(expression))($scope); 
        }
      };
    }
    

提交回复
热议问题