如何在使用ng-options时使用ng-selected

时间:2017-06-26 10:41:28

标签: angularjs

如何在下拉列表中使用ng-options时使用ng-selected。

请参阅下面的插件。

gcc -std=c11 -Wall -Wextra -pedantic

https://plnkr.co/edit/XKJACU3N7iHclAYJZoDT

(ns my.service.server
(:refer-clojure :exclude [sort find])
(:require [monger.core :as mg]
        [monger.query :refer :all])
(:import com.mongodb.ReadPreference))

(let [conn (mg/connect)
  db   (mg/get-db conn "monger-test")
  coll "scores"]
;; reads from primary (master) to guarantee consistency
;; (at the cost of putting extra load on the primary)
(with-collection db coll
(find {:email "joe@example.com"})
(read-preference (ReadPreference/primary))))

2 个答案:

答案 0 :(得分:0)

请从角度https://docs.angularjs.org/api/ng/directive/ngSelected

检查此API
  

注意:ngSelected不与select和ngModel交互   指令,它只在元素上设置selected属性。如果你   在select上使用ngModel时,不应该使用ngSelected   选项,因为ngModel将设置选择值和选定的选项。

答案 1 :(得分:0)

ng-selected应该进入选项标签而不是选择标签。您应该为所选模型使用ng-model而不是数组,请使用您的脚本查看下面的更新:

app.controller('MainCtrl', function($scope) {
    $scope.major_head_list = [
        {id: '1', name: 'CAPEX'},
        {id: '2', name: 'Supplier Advance'},
        {id: '3', name: 'Salary Payable'},
        {id: '4', name: 'Employee Benefits'},
        {id: '5', name: 'TDS'}
    ];
    $scope.minor_head_list = [
        {id: '1', name: 'Office Equipment'},
        {id: '2', name: 'Furniture & Fixtures'},
        {id: '3', name: 'OPEX'},
        {id: '4', name: 'Insurance'},
        {id: '5', name: 'TDS on Vendors'}
    ];

    $scope.dist_list_e = [
        {major_id: '1', minor_id: '1', gi_code: '21810030000'},
        {major_id: '1', minor_id: '2', gi_code: '21810040000'},
        {major_id: '2', minor_id: '3', gi_code: '21810050000'},
        {major_id: '5', minor_id: '5', gi_code: '21810060000'}
    ];

    $scope.selected_major_head_list = $scope.major_head_list[0];
});

并选择标签:

<div class="input-field col s12">
    <select name="major_head" class="browser-default" ng-change="major_change($index)"
        data-ng-model="selected_major_head_list" data-ng-options="I.id as I.name for I in major_head_list track by I.id" >
        <option value="">Choose your option</option>
    </select>
</div>