最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Populate dropdown using curly braces + angularjs - Stack Overflow

programmeradmin6浏览0评论

the curly braces syntax interpolation is used to bind the data from model to view in angularjs.

Generally we display text using it.

Can we populate dropdownlist using the {{}} syntax in any way?

the curly braces syntax interpolation is used to bind the data from model to view in angularjs.

Generally we display text using it.

Can we populate dropdownlist using the {{}} syntax in any way?

Share Improve this question edited Sep 12, 2013 at 12:48 Richard Ev 54.2k61 gold badges194 silver badges281 bronze badges asked Sep 12, 2013 at 12:47 Tushar SharmaTushar Sharma 3471 gold badge3 silver badges11 bronze badges 2
  • 4 Why not use ng-options? – AlwaysALearner Commented Sep 12, 2013 at 12:48
  • But along with ng-options we have to use ng-model. And I am searching for a technique, which can allow us to populate data without ng-model – Tushar Sharma Commented Sep 12, 2013 at 13:53
Add a ment  | 

4 Answers 4

Reset to default 5
<select>
    <option ng-repeat="item in items" value="item.value">{{item.title}}</option>
</select>

You certainly can populate a dropdownlist using the curly braces in angular, but it won't have the expected effect.
If you want to have the data-binding in your select box, you should use the select directive which writes the option tags in a similar way the ng-repeat directive would.
Here's an example:

js:

$scope.selectables = [
    { label: 'A', value: 1},
    { label:'B', value: 2},
    { label: 'C', value: 3}
];

// this is the model that's used for the data binding in the select directive
// the default selected item
$scope.selectedItem = $scope.selectables[0];

html:

<select 
   ng-model="selectedItem" 
   ng-options="o.label for o in selectables">
</select>
<p>Selected item value: {{selectedItem.value}}</p>

Here's a demo to clear things up: http://jsfiddle/gion_13/TU6tp/

Well, you can populate a dropdownlist this way :

<select ng-model="myItem" ng-options="item.name for item in items"></select>

The selected item will be stored in $scope.myItem.

Check this page : http://docs.angularjs/api/ng.directive:select

Hope it helps.

You can do something like this:

<select ng-model="searchDropdown" ng-class="{active: isDropdownActive}">
            <option ng-repeat="item in data.results" value="{{ item[settings.filterOptions[0].value] }}" >{{ item[settings.filterOptions[0].value] }}</option>
</select>
发布评论

评论列表(0)

  1. 暂无评论