myAppModule.controller('FrmController', ['$scope',function($scope){ $scope.colors = [{name:'black', shade:'dark'},{name:'white', shade:'light'},{name:'red', shade:'dark'},{name:'blue', shade:'dark'},{name:'yellow', shade:'light'}]; $scope.myColor = 'red';}]);
<form ng-controller="FrmController">
<select ng-model="m.myColor" ng-options="color.name as color.name for color in colors">
</select>
</form>
----------------------------
$scope.m = $scope.colors[0];
<select ng-model="m" ng-options="***color ***as color.name for color in colors"> 这样居然可以一次获取到 color 对象的全部属性值;
<select ng-model="m" ng-options="***color ***as color.name for color in colors"> 这样居然可以一次获取到 color 对象的全部属性值;
<!doctype html>
<
html
>
<
head
>
<
meta
charset
=
"UTF-8"
>
<
title
>test</
title
>
<
script
type
=
"text/javascript"
src
=
"angular.min.js"
></
script
>
<
script
>
var myApp = angular.module("myApp", []);
myApp.controller("testCtrl", function($scope){
$scope.data = [{id:1,value:'hello'},{id:2,value:'bye'},{id:3,value:'hmmmm'}];
//$scope.selectValue = $scope.data[0].id;
});
</
script
>
</
head
>
<
body
ng-app
=
"myApp"
>
<
form
ng-controller
=
"testCtrl"
>
<
select
ng-model
=
"selectValue"
ng-options
=
"item.id as item.value for item in data"
></
select
><
br
/><
br
/>
<
div
ng-switch
=
"selectValue"
>
<
div
ng-switch-when
=
"1"
>hello</
div
>
<
div
ng-switch-when
=
"2"
>bye</
div
>
<
div
ng-switch-when
=
"3"
>hmmmm</
div
>
<
div
ng-switch-default>ah?</
div
>
</
div
>
</
form
>
</
body
>
</
html
>
|
也可以不用switch用ng-show/hide或者ng-if这样的。