处理表达式的最佳方法是什么? 我知道我可以添加很多代码来解决这个问题,但是想知道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.
答案 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;