如何根据值

时间:2015-08-18 08:34:26

标签: angularjs

我是棱角分明的新手

我坚持以下问题

我有一个选择下拉,由AJAX调用填充我的要求是默认选择选项,我知道如何做JQUERY

简而言之请告诉我相当于$(#id).val()的角度 以下是摘录

 <select  id="eventypeee" ng-change="appendselected()"
  ng-model="eventtypelistforcity.selected" 
  ng-options="option.eventId as option.eventAliasName for option in eventtypelistforcity | orderBy:'eventAliasName'">
  <!-- test -->
</select> 

我希望选择的eventid = 11被选中

2 个答案:

答案 0 :(得分:0)

如果为所选选项创建单独的模型,而不是将其指向用于创建选项的同一对象,则会更容易。试试这个:

DEMO

<强> HTML

<body ng-controller="MainCtrl">
    <select  
      id="eventypeee" 
      ng-change="appendselected()"
      ng-model="selectedOption" 
      ng-options="option.eventId as option.eventAliasName for option in eventtypelistforcity | orderBy:'eventAliasName'"
    ></select> 

    <pre>{{selectedOption}}</pre>
  </body>

<强> JS

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {

  $scope.eventtypelistforcity = [ 

    {
      eventId: 1,
      eventAliasName: 'foo'
    },

    {
      eventId: 2,
      eventAliasName: 'bar'
    },

    {
      eventId: 3,
      eventAliasName: 'baz'
    } 

  ];

  // defined a model for your selected option 
  // you access the selected option through this..
  $scope.selectedOption = $scope.eventtypelistforcity[0].eventId;


  $scope.appendSelected = function(){

  }


});

答案 1 :(得分:0)

您可以像这样简单地使用ngSelected指令:

<select id="eventypeee" ng-change="appendselected()" ng-model="eventtypelistforcity.selected">
    <option ng-selected="option.eventId==11" data-ng-repeat="option in eventtypelistforcity" value="{{ option.eventId }}">{{ option.eventAliasName }}</option>
</select>

JS:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {

    $scope.eventtypelistforcity = [ 
        {
           eventId: 11,
           eventAliasName: 'Event11'
        } ,
        {
           eventId: 1,
           eventAliasName: 'foo'
        },
        {
           eventId: 2,
           eventAliasName: 'bar'
        }

    ];

    //Define your model corresponding to your select value
    $scope.eventtypelistforcity.selected $scope.eventtypelistforcity[0].eventId;

});

DEMO:Plunker