繁体   English   中英

AngularJS表达式中的表达

[英]AngularJS Expression in Expression

有没有办法让AngularJS评估模型数据中的表达式?

HTML:

<p>
{{Txt}}
</p>


模型:

    { 
     Txt: "This is some text {{Rest}}"
    }

    {
      Rest: "and this is the rest of it."
    }

最终的结果是: This is some text and this is the rest of it

您可以使用$interpolate服务来插入字符串...

function ctrl ($scope, $interpolate) {
    $scope.Txt = "This is some text {{Rest}}";
    $scope.Rest = "and this is the rest of it.";
    $scope.interpolate = function (value) {
        return $interpolate(value)($scope);
    };
}

<div ng-app ng-controller="ctrl">
    <input ng-model="Txt" size="60" />
    <p>{{ Txt }}</p>
    <p>{{ interpolate(Txt) }}</p>
</div>

的jsfiddle

您可以在括号内执行JS:

<p>{{Txt + ' ' + Rest}}</p>

或者创建一个返回所需文本的函数:

$scope.getText = function() {
  return $scope.Txt + ' ' + $scope.Rest;
}

<p>{{getText()}}</p>

当您使用javascript模型时,您可以执行以下简单操作:

var rest = 'and this is the rest of it';
$scope.txt = 'This is some text ' + rest;

在视图中,请保留<p>{{txt}}</p>

或者,您可以将表达式串在一起<p>{{txt}} {{rest}}</p>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM