gpt4 book ai didi

javascript - 如何使用 ng-init 和 ng-repeat

转载 作者:行者123 更新时间:2023-11-30 15:45:34 28 4
gpt4 key购买 nike

我正在创建一个网络应用程序,我在其中使用下拉菜单和 angularjs 来更新我的数据库,

这是我的下拉列表语法

<select ng-model="ucomname" ng-change="uucomname(o)">
<option ng-repeat="o in comnamelistfun" value="{{o.comname}}">{{o.comname}}</option>
</select>

此下拉列表中的第一个值变为空白,但我希望下拉列表的第一个值成为下拉列表的第一个值,当我使用 ng-init 时我无法在 ng-model 中发送任何想法?

最佳答案

你可以使用它。 http://plnkr.co/edit/1EVs7R20pCffewrG0EmI?p=preview

    (function() {
'use strict';

angular
.module('app', [])
.controller('HomeCtrl', HomeCtrl);

HomeCtrl.$inject = ['$scope'];

function HomeCtrl($scope) {

$scope.cities = [
{ id: 1, name: 'London' },
{ id: 2, name: 'Chicago' },
{ id: 3, name: 'Moscow' },
{ id: 4, name: 'Mumbai' },
{ id: 5, name: 'Casablanca' }
];

// Pre-select city by id
$scope.selectedCityId = 1;

// Pre-select city by object
$scope.selectedCity = { id: 1, name: 'Casablanca' };

}
})();

<!DOCTYPE html>
<html ng-app="app">

<head>
<link rel="stylesheet" href="style.css" />
</head>

<body ng-controller="HomeCtrl">
<h3>Data List</h3>

<table>
<tbody>
<tr ng-repeat="city in cities">
<td>{{city.id}}</td>
<td>{{city.name}}</td>
</tr>
</tbody>
</table>

<h3>Selected City (by Id)</h3>
selectedCityId: <strong>{{selectedCityId}}</strong>
<br/>
<br/>
<label>Select City
<select ng-model="selectedCityId" ng-options="city.id as city.name for city in cities">
<option value="">-- Select City --</option>
</select>
</label>

<hr/>

<h3>Selected City (by object)</h3>
selectedCity: <strong>{{selectedCity}}</strong>
<br/>
<br/>
<label>Select City
<select ng-model="selectedCity" ng-options="city as city.name for city in cities track by city.id">
<option value="">-- Select City --</option>
</select>
</label>

<hr/>

<!-- Scripts -->
<script data-require="angular.js@1.5.6" data-semver="1.5.6" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script src="HomeCtrl.js"></script>

</body>
</html>

关于javascript - 如何使用 ng-init 和 ng-repeat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40123425/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com