當我選擇名稱的國家/地區時,有一個國家和州的下拉串列我必須傳遞國家/地區的 id,但是在單擊事件之后它應該傳遞 id 但顯示為名稱我該怎么做請指導
<md-select name="state" id="state" placeholder="Select a state" md-no-asterisk="true"
ng-change="getCounties(state)" ng-model="state">
<md-option ng-repeat="item in state track by id"
value="{{item.id}}">
{{item.name}}
</md-option>
</md-select>
{{AddressState}} /* This should be name instead of value how can i display this ? */
<div class="clearfix" ng-click="edit()">
Edit</a></div>
controller.ts :
$scope.AddressState = state;
uj5u.com熱心網友回復:
不要state用作您的ng-model,因為您state已經是您的資料源。考慮改為使用物件來捕獲所有用戶輸入,并將state其作為屬性。您的 ng-model 將捕獲用戶選擇的 ID,這是您想要的,您可以使用它來獲取狀態名稱。
angular.module('myApp', [])
.controller('myCtrl', ['$scope', function($scope) {
$scope.userInput = {
state: {}
};
$scope.state = [{
"name": "Califoria",
"id": 1
}, {
"name": "Texas",
"id": 2
}]
$scope.getCounties = function() {
// $scope.userInput.state is updated autmatically with the ID
console.log($scope.userInput)
// the associated data can be found using
let state = $scope.state.find(s => s.id == $scope.userInput.state)
$scope.selectedState = state.name; // for display
}
}]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<select ng-model="userInput.state" ng-change="getCounties()">
<option ng-repeat="item in state" value="{{item.id}}">
{{item.name}}
</option>
</select>
<p> {{selectedState}} </p>
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/465049.html
標籤:angularjs
