What are the differences between ng-repeat and ng-options and why do they not behave the same way?

后端 未结 5 989
盖世英雄少女心
盖世英雄少女心 2020-11-27 20:31

How do ng-options and ng-repeat differ?

In the following code, I have an ng-repeat that iterates through a list of people:

5条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-27 20:40

    ng-options is the directive which is designed specifically to populate the items of a dropdown list. One major advantage using ng-options for the dropdown is, it allows us to pass the selected value to be an object. Whereas, using ng-repeat the selected value can only be string.

    
    

    Id of the selected item is : {{selectedPerson}}

    By Using the above method, the value of the selectedPerson is always a string.

    
     

    Id of the selected item is : {{selectedPerson.id}}

    Name of the selected item is : {{selectedPerson.name}}

    Here, the value of the selected person is an object so we can print any required field of the object by passing the complete object.

提交回复
热议问题