如何使用角度中的选择框更改图像?

时间:2015-02-05 14:05:51

标签: angularjs

嗨朋友们,我在这里通过选择不同的值来改变文本的值,但我希望图像也能相应地改变。

这里小提琴:http://jsfiddle.net/TU6tp/44/

我们是否应该像文字一样包含图像,同时更改图像也会改变?

代码

  <div ng-controller="myCtrl">
  <select 
   ng-model="selectedItem" 
   ng-options="S.label for S in selectables">
  </select>
 <p>Selected item value: {{selectedItem.value}}</p>
 <p>Selected item image: {{selectedItem.img}}</p>
</div>

我看到另外一个链接http://jsfiddle.net/wktwL/我也很困惑如何在选择时更改图像。请有人帮助我。

1 个答案:

答案 0 :(得分:3)

http://jsfiddle.net/TU6tp/45/, 只需将图像的值放入img元素

的src中
<img src='{{selectedItem}}'/>



$scope.selectables = [
        { label: 'A', value: 'http://upload.wikimedia.org/wikipedia/commons/a/af/Bonsai_IMG_6426.jpg'},
        { label:'B', value:'http://hdwallpaper.freehdw.com/0001/nature-landscapes_widewallpaper_nature-s-beauty_9645.jpg'}
    ];

您可选择数组的值应包含图像的路径