简体   繁体   English

如何在Angular JS的输入文本字段中显示下拉列表的选定值

[英]how to display selected value of dropdown in input text fields in angular js

html file: html文件:

<select ng-model="shipping" ng-options="shipping.shipping for shipping in shipAddress">
 <option value=''>--Select Address --</option>
</select>

<form name="shippingForm" class="form-horizontal" role="form">
 <div class="form-group">
    <p class="control-p col-sm-2" for="Address">Address Line1</p>
    <div class="col-sm-10">
        <input type="text" name="Address" placeholder="Address Line1"
         ng-model="shipping.addressLine1" class="input-width" ng-init ="shipping.addressLine1"/>
   </div>
 </div>

 <div class="form-group">
      <p class="control-p col-sm-2" for="Address2">Address Line2:</p>
      <div class="col-sm-10">
           <input type="text" name="AddressLine" placeholder="Address Line2" ng-model="shipping.addressLine2" class="input-width" />
        </div>
      </div>
  </form>

js file: js文件:

if (data){  
          console.log(data);      
          $scope.addressToShip = data; 
          var ShipAddress=[];
          storeLocally.set('ship Info', data);
          if(data.addressLine2 == null){
            $scope.shipAddress = data.map(function(address) {
                  return {
                  shipping: address.addressLine1 
                  };

               });
           }
          else{
          $scope.shipAddress = data.map(function(address) {
              return {
              shipping: address.addressLine1 +', '+ address.addressLine2
              };

           });
      } 
      }
 },
    function (data) {
        //if (data.status == 500) {
        //  $scope.addressError = "Oops! No Address Found!";    
        };    

data: 数据:

{0 :"123 waller st,suite#220"},
{1 :"323 waller st,suite#230"}

The problem is the form I am using intially to save data. 问题是我最初使用的表单来保存数据。 Once data is saved to db it is coming in dropdown. 数据保存到数据库后,它就会出现在下拉列表中。 After choosing one value from dropdown it should come to text fields. 从下拉列表中选择一个值后,它应该出现在文本字段中。

I have tried so far ng-model by using same variable names,even though form's ng-model and dropdown's ng-model have same variable ie shipping.. But it didn't work. 到目前为止,我已经通过使用相同的变量名称尝试了ng-model,即使form的ng-model和dropdown的ng-model具有相同的变量(即shipping)。但是它没有用。 Please help me out what I am missing in here. 请帮助我,我在这里想念的东西。

If I understand your goal and you data's schema, you need to change the ng-model of the input[name="Address"] to shipping.addressLine1.shipping . 如果我了解您的目标和数据的架构,则需要将input[name="Address"]ng-model更改为shipping.addressLine1.shipping

I get this idea according the data's structure that came from the .map : 我根据来自.map的数据结构得到了这个想法:

return {
     shipping: address.addressLine1 
};

You can see the structure when you pick a option from the select - the model will displayd and you can see that the structure is (for example): 当您从select一个选项时,您可以看到结构-模型将显示,并且您可以看到该结构是(例如):

{
  "addressLine1": {
    "shipping": "123 waller st"
  }
}

If you have a question about this, let me know. 如果您对此有疑问,请告诉我。

I tried to simulate the situation without the server, hopefully I simulate it right. 我尝试在没有服务器的情况下模拟这种情况,希望我能正确模拟。

Now, to the code: 现在,到代码:

 angular.module('myApp', []). controller('ctrl', function($scope) { var data = [{ addressLine1:"123 waller st", addressLine2:"suite#220" }]; if (data) { console.log(data); $scope.addressToShip = data; var ShipAddress = []; //storeLocally.set('ship Info', data); if (data.addressLine2 == null) { $scope.shipAddress = data.map(function(address) { return { shipping: address.addressLine1 }; }); } else { $scope.shipAddress = data.map(function(address) { return { shipping: address.addressLine1 +', '+ address.addressLine2 }; }); } } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myApp" ng-controller="ctrl"> <select ng-model="shipping.addressLine1" ng-options="shipping.shipping for shipping in shipAddress"> <option value=''>--Select Address --</option> </select> <br /> <pre> {{shipping | json}}<br /> {{shipping.addressLine1 | json}} </pre> <form name="shippingForm" class="form-horizontal" role="form"> <div class="form-group"> <p class="control-p col-sm-2" for="Address">Address Line1</p> <div class="col-sm-10"> <input type="text" name="Address" placeholder="Address Line1" ng-model="shipping.addressLine1.shipping" class="input-width" ng-init ="shipping.addressLine1"/> </div> </div> <div class="form-group"> <p class="control-p col-sm-2" for="Address2">Address Line2:</p> <div class="col-sm-10"> <input type="text" name="AddressLine" placeholder="Address Line2" ng-model="shipping.addressLine2" class="input-width" /> </div> </div> </form> </div> 

Did you try using AngularJS' ng-change attribute in the drop down? 您是否尝试过在下拉菜单中使用AngularJS的ng-change属性?

Try like this: 尝试这样:

<select ng-model="shipping" ng-options="shipping.shipping for shipping in shipAddress" ng-change="someFunction(shipping.shipping)">
  <option value=''>--Select Address--</option>
</select>

In your controller add the following function: 在您的控制器中添加以下功能:

$scope.someFunction = function(shipping) {
  $scope.shipping.addressLine1 = // Assign value from the shipping variable here
  $scope.shipping.addressLine2 = // Assign value from the shipping variable here
}

Hope this helps! 希望这可以帮助!

Here is a working jsFiddle https://jsfiddle.net/ashishU/umn8or3g/1/ 这是一个工作的jsFiddle https://jsfiddle.net/ashishU/umn8or3g/1/

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

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