Angular - 数据绑定问题

时间:2016-10-18 15:33:28

标签: angularjs input data-binding initialization

所以我想要的主要功能就在这里,它是从下拉菜单中选择一个选项并让它填充我的输入字段。

JSFiddle:

<div ng-app="app" ng-controller="MainCtrl">

                            

我想解决两件事:

  1. 输入输入(“电子邮件主题”)字段时,我不想更改下拉菜单选项。

  2. 我希望输入字段使用其占位符值(“电子邮件主题”)进行初始化,而不是使用“选择预设响应”初始化

  3. 我假设这意味着使输入字段具有单向数据绑定,但我不知道如何做到这一点,任何帮助都表示赞赏。

1 个答案:

答案 0 :(得分:1)

<div ng-app="app" ng-controller="MainCtrl">
  <input   ng-model="CannedResponse" placeholder="Email Subject"><!--change this-->
  <div class="item item-input item-select" >
    <div class="input-label">&nbsp;</div>
    <select ng-model="newSelectedITem" ng-options="CannedResponse as CannedResponse.label for CannedResponse in CannedResponses"
        ng-change="yourFunction()"> <!--change this-->
      <option value="{{item.value}}"> </option>
    </select>
  </div>
</div>

js代码

angular.module('app', [])
  .controller('MainCtrl', function($scope) {
    $scope.CannedResponses = [{
      label: 'Selet a Canned Response',
      value: 0
    }, {
      label: 'Hi there whats up',
      value: 1
    }, {
      label: 'Here is another response',
      value: 2
    }, {
      label: 'Why not select this one?',
      value: 3
    }];

     $scope.newSelectedITem = $scope.CannedResponses[0] //ADD THIS (X1) 
    $scope.CannedResponse='';//add this
    $scope.yourFunction = function() {//add this function
    $scope.CannedResponse = $scope.newSelectedITem.label;
     };

  });

查看我写的地方更改此。在那里你必须改变你的代码。