有没有办法让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。
This is some text and this is the rest of it
您可以使用该$interpolate服务来插值字符串…
$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