javascript - 如何将 ng-model 从 Angular 1.6 组件传递到输入元素

标签 javascript angularjs custom-component

我正在尝试创建一个 Angular 1.6 组件,它是输入标签的包装器。我正在创建提前输入或自动完成功能。我必须在 html 中使用 ng-model 来创建组件,这很好,但我希望在子输入元素中使用 ng-model。对于组件,您必须使用元素,并且不能像指令那样使用属性标签。我创建了一支代码笔来说明我正在尝试做的事情。

http://codepen.io/patrickliechty/pen/GWLZeX?editors=1011

我想使用 ngController 来更新输入元素中的值。在代码笔中,如果您键入输入,您将看到绑定(bind)的模型数据显示在输入元素下方。

这是代码:

angular.module('app', ['EntryField']);

angular.module('app').controller('DataController', ['$scope', function($scope) {
  $scope.fieldDataArray = [{"label": "First Name", "content": ""}, {"label": "Last Name", "content": ""}];
  console.log("$scope.fieldDataArray: ", $scope.fieldDataArray)
}]);

angular.module('EntryField', []).component('customAutoComplete', {
  template: '<input type="text" name="input" ng-model="$ctrl.ngModelController.$modelValue" autocomplete="off"/><br>[{{$ctrl.ngModelController.$viewValue}}]',
  require: {
    ngModelController: "ngModel"
  },
  bindings: {},
  controller: 'CustomAutocompleteController'
});

angular.module('EntryField').controller('CustomAutocompleteController', CustomAutoController);

CustomAutoController.$inject = ['$scope', '$element'];

function CustomAutoController($scope, $element) {
  let ctrl = this;
  
  ctrl.$onInit = function() {
    ctrl.ngModelController.$parsers.unshift(function (inputValue) {
      handleInputUpdate(inputValue);
    });
  }


  function handleInputUpdate(inputValue) {
    //Do autocomplete functionality
  }

}
<html ng-app="app">
  <head></head>
  <body>
    <div>Angular 1.x Auto Complete</div>
    
    <div ng-controller="DataController">
      <div ng-repeat="fieldData in fieldDataArray">
        <div>{{fieldData.label}}</div>
        <custom-auto-complete ng-model="fieldData.content"></custom-auto-complete> 
      </div>
    </div>
  
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.min.js"></script>
  </body>
</html>

最佳答案

我有一个非常有效的问题解决方案,但我想知道其他人是否有更好的解决方案。

var app = angular.module('app', ['EntryField']);

app.controller('DataController', ['$scope', function($scope) {
  $scope.fieldDataArray = [{
    "label": "First Name",
    "content": ""
  }, {
    "label": "Last Name",
    "content": ""
  }];
  console.log("$scope.fieldDataArray: ", $scope.fieldDataArray);
}]);

angular.module('EntryField', []);

angular.module('EntryField').component('customAutoComplete', {
  template: '<input type="text" name="input" ng-model="$ctrl.fieldData.content" autocomplete="off"/><br>[{{$ctrl.fieldData.content}}]',
  bindings: {
    fieldData: "="
  },
  controller: function($element, $timeout) {
    let ngModelController;
    this.$postLink = function() {
      if (!ngModelController) {
        ngModelController = angular.element($element.find('input')).controller('ngModel');
        ngModelController.$parsers.unshift(function(inputValue) {
          handleInputUpdate(inputValue);
          return inputValue;
        });
       
        function handleInputUpdate(inputValue) {
          console.log("!!!!!!!!!!!!!!!!!Got some input: ", inputValue);
        }
      }      
    }
  }
});
<!DOCTYPE html>
<html ng-app="app">

  <head>
    <meta charset="utf-8" />
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.js"></script>
  </head>

  <body>
    <div ng-controller="DataController">
      <div ng-repeat="fieldData in fieldDataArray">
        <div>{{fieldData.label}}</div>
        <custom-auto-complete field-data="fieldData"></custom-auto-complete>
      </div>
    </div>
  </body>

</html>

关于javascript - 如何将 ng-model 从 Angular 1.6 组件传递到输入元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43221524/

相关文章:

javascript - 如何正确处理来自内容脚本的 chrome 扩展更新

javascript - 在 ionic 中添加按钮清除值 ion-datetime

javascript - Dynamics CRM 2011 将数组作为参数传递

angularjs - 使用 Ionic 作为前端、Django 作为后端开发移动应用程序

delphi - 是否可以向 delphi 组件添加自定义功能,以便在按下特定键时触发?

android - 如何预览自定义 android 组件布局?

delphi - 更改父容器会导致 Createwnd 再次被调用,Delphi 6

javascript - 每 10 分钟显示一次插页式广告

angularjs - 监视使用 $http get 来检查函数被调用的工厂

即使没有 application/json 内容类型,AngularJS 也会继续执行预检请求