2

I am trying to evaluate an expression inside expression using angularJS.Please let me know how to do it?

Problem:

I have two objects department and facility and i want to display departments and facilities in accordions. So, i am able to pass department object and facility object. Now the problem arises.As the department object has property department_name and facility has facility_name.I passed the department_name and facility_name to label attribute in directive and trying to use in accordion html like this {{item.{{label}}}}.I don't think this is right. But please let me know how can i do this.

Thanks in advance

In HTML:

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

In Accordion 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 file:

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

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

Directive:

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

1 Answer 1

4

Use square brackets:

{{item[label]}}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Micheal. But how is it working. Could you explain ? It would be really helpful for me.
That's javascript basic. You can access an object property as object.foo or object ['foo']. See Property Access for more information.
Yeah, I got it yesterday only, just forgot the basics key[value] . Anyway thanks Michael .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.