在模板中将字符串与范围变量连接起来

时间:2016-12-21 09:22:56

标签: javascript html angularjs

我有一个案例,我希望将字符串与作用域定义的变量连接起来,如下所示:

$scope.variable1 = "Test Name";
$scope.variable2 = "Test 2 Name";

<a href="#">{{variable1? "({{variable1}})" : variable2}}</a>

我想在设置variable1然后显示&#34;(variable1)&#34;否则&#34;变量2&#34;。

3 个答案:

答案 0 :(得分:2)

变化:

<a href="#">{{variable1? "({{variable1}})" : variable2}}</a>

要:

<a href="#">{{variable1 ? "(" + variable1 + ")" : variable2}}</a>

答案 1 :(得分:1)

试试{{variable1? &#39;(&#39; +变量1 +&#39;)&#39; :variable2}}

答案 2 :(得分:1)

试试这个。

&#13;
&#13;
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<body>

    <div ng-app="myApp" ng-controller="myCtrl">

        <a href="#">{{variable1 ? "(" + variable1 + ")" : variable2}}</a>

    </div>

    <script>
        var app = angular.module('myApp', []);
        app.controller('myCtrl', function ($scope) {
            $scope.variable1 = "Test Name";
            $scope.variable2 = "Test 2 Name";
        });
    </script>


</body>

</html>
&#13;
&#13;
&#13;