表达式引用表达式引用

时间:2014-07-14 09:15:55

标签: angularjs

处理表达式的最佳方法是什么? 我知道我可以添加很多代码来解决这个问题,但是想知道AngularJs API中是否有一个简单的解决方案。

<h1>Hello {{yourName}}!</h1> <!-- works fine, shows 'Jim' -->
<h1>Hello {{yourTest}}!</h1> <!-- will show {{test}}, but I want 'Jack' -->

$scope.yourName = "Jim";
$scope.test = "Jack";
$scope.yourTest = "{{test}}";  //This must be a string in my case.

2 个答案:

答案 0 :(得分:1)

您可以使用 $interpolate

$插值

  

将带有标记的字符串编译为插值函数。这个   HTML $ compile服务使用服务进行数据绑定。看到   $ interpolateProvider用于配置插值标记。

看看这个

<强> Working Demo

app.controller('Controller', function ($scope, $interpolate) {

$scope.yourName = "Jim";
$scope.test = "Jack";
$scope.yourTest = $interpolate("{{test}}")($scope);
});

答案 1 :(得分:0)

请试试这个:

<h1>Hello {{yourName}}!</h1> <!-- works fine, shows 'Jim' -->
<h1>Hello {{yourTest}}!</h1> <!-- will show {{test}}, but I want 'Jack' -->

$scope.yourName = "Jim";
$scope.test = "Jack";
$scope.yourTest = $scope.test;