簡體   English   中英

Angular JS-嵌套模板(雙大括號{{}})

[英]Angular JS - nesting templates (double curly brackets {{}} )

有沒有嵌套角度模板的方法?

例如具有以下變量:

$scope.myVar = 'hello {{name}}, you weigh {{weight}}'
$scope.name = '';
$scope.weight = '';

並讓html文件看起來像這樣:

<h1>{{myVar}}</h1>
<div>
    Your Name:
    <input ng-model="name" type="text">
</div>
<div>
    Your Weight:
    <input ng-model="weight" type="text">
</div>

然后讓angular從myVar動態設置名稱和權重模板嗎?

我認為您在尋找毫無意義。 您可以定義如下所示的函數,然后在標題中調用此函數。

$scope.myVar = function() {
    return 'hello ' + $scope.name + ' you weigh '+ $scope.weight;
}

 var app = angular.module('app',[]); app.controller('mainController',['$scope', function($scope){ $scope.name = "test"; $scope.weight = 100; $scope.myVar = function() { return 'hello ' + $scope.name + ' you weigh ' + $scope.weight; }; }]); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <html> <head> </head> <body ng-app="app"> <div ng-controller="mainController"> <h1>{{myVar()}}</h1> <div> Your Name: <input ng-model="name" type="text"/> </div> <br/> <div> Your Weight: <input ng-model="weight" type="text"/> </div> </div> </body> </html> 

我最終意識到自己有范圍錯誤。 因此,為解決綁定錯誤,我使用了$ interpolate。 請參閱: 如何在另一個表達式內求出angularjs表達式

當然,您會這樣做:

<h1>Hello {{name}} you weigh {{weight}}</h1>
<div>
    Your Name:
    <input ng-model="name" type="text">
</div>
<div>
    Your Weight:
    <input ng-model="weight" type="text">
</div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM