简体   繁体   English

ng-model作为ng-init输入的值

[英]ng-model as value for ng-init input

I'm trying to figure out on how can I set the value of my input based on the ng-model of my another input, Here is what I did: 我试图弄清楚如何根据另一个输入的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> 

As you can see, my second input literally show {{register.regData.branch.chapter_name}} instead of showing chapter_name , How can I show the chapter_name in my second input? 如您所见,我的第二个输入从字面上显示了{{register.regData.branch.chapter_name}}而不是chapter_name ,我该如何在我的第二个输入中显示Chapter_name?

There are slight changes in HTML. HTML稍有变化。 You can refer JsFiddle 你可以参考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>

If I understand right, you need something like this? 如果我理解正确,您需要这样的东西吗? https://stackblitz.com/edit/angularjs-52l5k7 https://stackblitz.com/edit/angularjs-52l5k7

If you want to reuse your ng-model you should use ng-bind to bind data. 如果要重用ng-model,则应使用ng-bind绑定数据。 https://docs.angularjs.org/api/ng/directive/ngBind https://docs.angularjs.org/api/ng/directive/ngBind

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM