0

私のコントローラーでは、次を割り当てます。

$scope.currentThing.data

そして、私のテンプレートでは時々必要です

currentThing.data.response[0].hello

そして時折

currentThing.data.otherStuff[0].goodbye
//or
currentThing.data.anotherThing[0].goodMorning

したがって、次のようなテンプレートでこの変数のショートカットを直接作成できるかどうか疑問に思っていました。

{{response = currentThing.data.response[0]}}

こんな風に使えるように{{response.hello}}

一般に、テンプレートから一時変数を割り当てることは可能ですか? データバインディングは必要ありません。テンプレートを生成するためだけに必要であり、その後永久に消える可能性があります

4

3 に答える 3

2

ここのようにコントローラでそれを行うことができます: http://jsbin.com/moyuhe/1/edit

app.controller('firstCtrl', function($scope){
 $scope.currentThing = {

   data: [

     {response:[
       {hello:1},
       {hello:2}
     ]}
   ]

 };
 $scope.temp = $scope.currentThing.data[0];

});

HTML:

 <div ng-controller="firstCtrl">
  {{temp.response |json }}
      </div>
于 2014-07-17T16:01:47.367 に答える
0

はい、次のような構文を使用して可能です

{{ variable = ( expression ) }}

HTML テンプレートのどこでも (ng-init一部の提案だけでなく)。

これは、計算された変数を何度も使用する必要がある場合に非常に役立ちます。毎回計算する必要はありません。

いくつかの例

<!-- can use it before -->
<p> Calculated value is {{calculated}} </p>

<div ng-repeat=" item in calculated = ( allItems | filter1 | filter2 | filter3 ) ">
   {{item}} 
</div>

<!-- can use it after-->
<p> Calculated value is still {{calculated}} </p>

編集:あなたの場合 {{response = ( currentThing.data.response[0] ) }}

于 2014-07-17T17:48:01.763 に答える