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

javascript - Angularjs 2: select not updating with selected value in ion-select Ionic 2? - Stack Overflow

programmeradmin2浏览0评论

I have below code which is working so far, but have small problem which I cannot figure it out. It's not updating (set the selected value into ion-select) ion-select after user chooses one option? Basically UI is not updating with selected value?

<ion-item>
<ion-label>Classifications</ion-label>
<ion-select [(ngModel)]="selectedItem" #item (change)="onChange(selectedItem)">
<ion-option *ngFor="#item of items" [value]="item">{{item}}</ion-option>
</ion-select>
</ion-item>

onChange(selectedItem) {
   console.log('Selected item: '+selectedItem);
}

Out put properly displays as user select, any ideas what I'm missing here?

update

<ion-item> 
  <ion-label>Gender</ion-label> 
  <ion-select [(ngModel)]="gender"> 
    <ion-option value="f" checked="true">Female</ion-option> 
    <ion-option value="m">Male</ion-option> 
  </ion-select> 
</ion-item>

I have below code which is working so far, but have small problem which I cannot figure it out. It's not updating (set the selected value into ion-select) ion-select after user chooses one option? Basically UI is not updating with selected value?

<ion-item>
<ion-label>Classifications</ion-label>
<ion-select [(ngModel)]="selectedItem" #item (change)="onChange(selectedItem)">
<ion-option *ngFor="#item of items" [value]="item">{{item}}</ion-option>
</ion-select>
</ion-item>

onChange(selectedItem) {
   console.log('Selected item: '+selectedItem);
}

Out put properly displays as user select, any ideas what I'm missing here?

update

<ion-item> 
  <ion-label>Gender</ion-label> 
  <ion-select [(ngModel)]="gender"> 
    <ion-option value="f" checked="true">Female</ion-option> 
    <ion-option value="m">Male</ion-option> 
  </ion-select> 
</ion-item>
Share Improve this question edited Feb 28, 2016 at 17:08 Günter Zöchbauer 658k233 gold badges2.1k silver badges1.6k bronze badges asked Feb 28, 2016 at 16:37 happycoderhappycoder 9773 gold badges14 silver badges30 bronze badges 9
  • Is onChange(selectedItem) being called? selectedItem should already be updated by <ion-select [(ngModel)]="selectedItem". (change)="onChange(selectedItem) shouldn't be necessary. – Günter Zöchbauer Commented Feb 28, 2016 at 16:40
  • yes, onChange(selectedItem) is called, but if I removed that line how do I get selected value, thus console.log('Selected item: '+selectedItem); in constructor gives this ReferenceError: selectedItem is not defined. – happycoder Commented Feb 28, 2016 at 16:46
  • To get rid of the error you'd need to change (change)="onChange(selectedItem)"> to (change)="onChange($event)"> or (change)="onChange(item.value)"> (I don't know what the property names of <ion-item> actually are or what value it provides for the event. – Günter Zöchbauer Commented Feb 28, 2016 at 16:49
  • You could also try if changing the event (ngModelChange)="onChange($event)"> also calls onChange(). – Günter Zöchbauer Commented Feb 28, 2016 at 16:50
  • It didnt work. This simple example of select, which already selected female by default.<ion-item> <ion-label>Gender</ion-label> <ion-select [(ngModel)]="gender"> <ion-option value="f" checked="true">Female</ion-option> <ion-option value="m">Male</ion-option> </ion-select> </ion-item> – happycoder Commented Feb 28, 2016 at 17:07
 |  Show 4 more ments

2 Answers 2

Reset to default 6

For Ionic 2 beta 10 next works for me:

<ion-item>
    <ion-label>Fest</ion-label>
    <ion-select [(ngModel)]="festId" (ionChange)="festSelected($event, festId)">
        <ion-option *ngFor="let fest of festList" value="{{fest.id}}">
            {{fest.title}}
        </ion-option>
    </ion-select>
</ion-item>

I was having the same issues after adding (ionChange)="" it started working for me.

<ion-select [(ngModel)]="msgType" (ionChange)="getListOfMsg()">
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>