ng-model 作为 ng-init 输入的值
ng-model as value for ng-init input
我正在尝试弄清楚如何根据另一个输入的 ng-model 设置我的输入值,这是我所做的:
angular.module('selectExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.register = {
regData: {
branch: {},
},
bloodbankchapters: [
{_id:'5c014c999cc48c3b0057988b', chapter_name:"AB"},
{_id:'5c014c999cc48c3b0057988c', chapter_name:"A"},
],
};
}]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="selectExample" ng-controller="ExampleController">
<select ng-model="register.regData.branch" ng-options ="chapter.chapter_name for chapter in register.bloodbankchapters track by chapter._id">
<option ng-repeat="chapter in register.bloodbankchapters" >{{chapter.chapter_name}}</option>
</select>
<div>{{register.regData.branch.chapter_name}}</div>
<input type="text" ng-model="register.regData.branch_name" name="branch_name" ng-init="register.regData.branch_name='{{register.regData.branch.chapter_name}}'">
</div>
如您所见,我的第二个输入字面上显示 {{register.regData.branch.chapter_name}}
而不是显示 chapter_name
,如何在我的第二个输入中显示 chapter_name?
如果我没理解错的话,你需要这样的东西吗?
https://stackblitz.com/edit/angularjs-52l5k7
如果你想重用你的 ng-model,你应该使用 ng-bind 来绑定数据。
https://docs.angularjs.org/api/ng/directive/ngBind
HTML略有变化。可以参考JsFiddle
<div ng-app="selectExample" ng-controller="ExampleController">
<select ng-model="register.regData.branch" ng-options ="chapter.chapter_name for chapter in register.bloodbankchapters" ng-change="register.regData.branch_name=register.regData.branch.chapter_name">
<option ng-repeat="chapter in register.bloodbankchapters" >{{chapter.chapter_name}}</option>
</select>
<div>{{register.regData.branch.chapter_name}}</div>
<input type="text" ng-model="register.regData.branch_name" name="branch_name">
</div>
我正在尝试弄清楚如何根据另一个输入的 ng-model 设置我的输入值,这是我所做的:
angular.module('selectExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.register = {
regData: {
branch: {},
},
bloodbankchapters: [
{_id:'5c014c999cc48c3b0057988b', chapter_name:"AB"},
{_id:'5c014c999cc48c3b0057988c', chapter_name:"A"},
],
};
}]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="selectExample" ng-controller="ExampleController">
<select ng-model="register.regData.branch" ng-options ="chapter.chapter_name for chapter in register.bloodbankchapters track by chapter._id">
<option ng-repeat="chapter in register.bloodbankchapters" >{{chapter.chapter_name}}</option>
</select>
<div>{{register.regData.branch.chapter_name}}</div>
<input type="text" ng-model="register.regData.branch_name" name="branch_name" ng-init="register.regData.branch_name='{{register.regData.branch.chapter_name}}'">
</div>
如您所见,我的第二个输入字面上显示 {{register.regData.branch.chapter_name}}
而不是显示 chapter_name
,如何在我的第二个输入中显示 chapter_name?
如果我没理解错的话,你需要这样的东西吗? https://stackblitz.com/edit/angularjs-52l5k7
如果你想重用你的 ng-model,你应该使用 ng-bind 来绑定数据。 https://docs.angularjs.org/api/ng/directive/ngBind
HTML略有变化。可以参考JsFiddle
<div ng-app="selectExample" ng-controller="ExampleController">
<select ng-model="register.regData.branch" ng-options ="chapter.chapter_name for chapter in register.bloodbankchapters" ng-change="register.regData.branch_name=register.regData.branch.chapter_name">
<option ng-repeat="chapter in register.bloodbankchapters" >{{chapter.chapter_name}}</option>
</select>
<div>{{register.regData.branch.chapter_name}}</div>
<input type="text" ng-model="register.regData.branch_name" name="branch_name">
</div>