【问题标题】:AngularJS: Migrate Expression to Javascript (Coffeescript)AngularJS:将表达式迁移到 Javascript(Coffeescript)
【发布时间】:2014-07-12 02:52:52
【问题描述】:

我有一个表达式我想迁移到 javascript 以保持 $scope 中可用的对象值:

<dl class = "mortgage-information">
    <dt><abbr title = "Loan-to-value Ratio">LTV</abbr></dt>
    <dd>{{(total_financing ? total_financing : financing)/ property_value}}</dd>
</dl>

但是将相同的表达式迁移到 javascript 将始终导致 financing 而不是 total_financing (Coffeescript):

$scope.ltv = (if $scope.total_financing then $scope.total_financing else $scope.financing) / $scope.property_value

我一直在阅读the documentation on angular expressions to no avail。任何人都可以建议将表达式迁移到 javascript 的更好方法吗?

【问题讨论】:

    标签: javascript angularjs coffeescript


    【解决方案1】:
    $scope.ltv = ($scope.total_financing ? $scope.total_financing : $scope.financing) / $scope.property_value;
    

    这是你想要的吗?

    【讨论】:

    • 那不是 CoffeeScript。
    • 它没有被标记为 coffeescript ,当我回答它时它被标记为 javascript 。请阅读其他答案中的 cmets
    【解决方案2】:

    您的 javascript 代码完全错误。首先,javascript没有像visual basic或其他语言那样的if-then结构,语法是这样的:

    if(...) {
       doSomething
    } else {
       do someThingElse
    }
    

    您犯的另一个错误是您必须将表达式分配给变量,而不是 if-else 构造的语句。您可以按照 Nikhil 的建议使用三元运算符,或者,如果您对三元运算符不满意,可以使用如下函数:

    $scope.ltv = function() {
       if($scope.total_financing) {
          return $scope.total_financing; 
       } 
       return $scope.financing;
    }
    

    然后在 html 绑定中使用函数调用,例如 {{ltv()}}

    【讨论】:

    • 这是coffeescript,不是javascript...编辑问题
    • 那么你应该用你的标签指出这一点,我只看到 javascript 和 angularjs 选项卡,而你没有提到咖啡脚本。
    • 很公平。我现在有更新标签(coffeescript 仍然是 javascript)。
    • Coffeescript 不是 Javascript
    猜你喜欢
    • 2015-02-22
    • 2012-08-09
    • 2015-11-21
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    相关资源
    最近更新 更多