[英]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?
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>
如果我理解正确,您需要这样的东西吗? https://stackblitz.com/edit/angularjs-52l5k7
如果要重用ng-model,则应使用ng-bind绑定数据。 https://docs.angularjs.org/api/ng/directive/ngBind
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.