设置初始值ng-options

时间:2015-01-06 21:01:32

标签: angularjs

我想设置和初始值,使用ng-options,我的值会更改元素的高度和宽度,然后使用ng-repeat重复,我想知道如何解决这个问题,希望你伙计们可以帮助我

HTML:

<select
       ng-options="init as initSize for s.width + ' X ' + s.height  s in sizes"
       ng-model="bannerSizes">
<option value=""></option>
</select>

使用Javascript:

$scope.sizes = [
    { width: 320, height: 600, chatContainer: 430 },
    { width: 320, height: 320, chatContainer: 147 },
    { width: 336, height: 320, chatContainer: 147 },
    { width: 320, height: 320, chatContainer: 147 },
];
$scope.initSize = $scope.sizes[0];

1 个答案:

答案 0 :(得分:0)

您不需要像以前那样添加<option value=""></option>

ng-model设置为bannerSizes,应为initSize

检查演示链接;

http://jsfiddle.net/srm16hhb/1/

<强> HTML

<div ng-controller="MyCtrl">
    <select ng-options="s as (s.width + ' X ' + s.height) for s in sizes" ng-model="initSize"></select>
</div>

js

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

function MyCtrl($scope) {
    $scope.sizes = [{
        width: 320,
        height: 600,
        chatContainer: 430
    }, {
        width: 320,
        height: 320,
        chatContainer: 147
    }, {
        width: 336,
        height: 320,
        chatContainer: 147
    }, {
        width: 320,
        height: 320,
        chatContainer: 147
    }, ];
    $scope.initSize = $scope.sizes[0];
}