2015-06-19 98 views
2

我想用angularJS評估表達式內部的表達式。請讓我知道如何去做?如何在AngularJS中評估表達式中的表達式

問題:

我有兩個對象部門及設施,我想顯示手風琴部門和設施。所以,我能夠通過部門對象和設施對象。現在問題就出現了。由於部門對象具有屬性department_name,並且facility具有facility_name.I,所以我通過了department_name和 facility_name來標記指令中的屬性,並試圖在手風琴html中使用,就像這樣{{item。{{label}}}}。我不認爲這是正確的。但請讓我知道我該如何做到這一點。

在此先感謝

在HTML:

<accordion-directive object="department" label="department_name"> 
</accordion-directive> 
<accordion-directive object="facility" label="facility_name"> 
</accordion-directive> 

在手風琴HTML

<accordion-group ng-repeat="item in object> 
<accordion-heading> 
{{item.{{label}}}} // For departments it should be department[0].department_name 
       // For Facility it should be facility[0].facility_name 

</accordion-heading> 
</accordion-group> 

JSON文件:

{ 
    "department": [ 
        { 
        "department_name": "department 1", 
        }, 
        { 
        "department_name": "department 2", 
        } 
       ] 
     } 

     { 
    "facility": [ 
        { 
        "facility_name": "Facility 1" 
        }, 
        { 
        "facility_name": "Facility 2" 
        } 
       ] 
} 

指令:

  angular.module("app").directive('accordDirective', function() { 
      return { 
      restrict: 'E', 
      scope: { 
       object: '=', 
       label:'@' 
       }, 
      templateUrl: 'accordian.html',  
     } 

回答

4

使用方括號:

{{item[label]}} 
+0

感謝@Micheal。但它是如何工作的。你能解釋一下嗎?這對我來說真的很有幫助。 – mahi244

+0

這是JavaScript的基本。你可以通過object.foo或者object ['foo']'來訪問一個對象屬性。有關更多信息,請參見[Property Access](http://www.sitepoint.com/back-to-basics-javascript-object-syntax/)。 – Michael

+0

是的,我昨天才知道,只是忘記了基本鍵[值]。無論如何感謝邁克爾。 – mahi244