How to pass ng-model from Angular 1.6 Component to input element
NickName:user1007983 Ask DateTime:2017-04-05T11:56:10

How to pass ng-model from Angular 1.6 Component to input element

I am trying to create an Angular 1.6 component that is a wrapper for an input tag. I am creating a type ahead or autocomplete. I have to use ng-model in the html to create the component and that is fine, but I want that ng-model used in the child input element. With a component you have to use an element and can't use attribute tag like directives use. I have created a code pen to illustrate what I am trying to do.

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

I want to use ngController to update the value in the input element. In the code pen, if you type in the input, you will see the bound model data show up below the input element.

Here is the code:

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>

Copyright Notice:Content Author:「user1007983」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/43221524/how-to-pass-ng-model-from-angular-1-6-component-to-input-element

More about “How to pass ng-model from Angular 1.6 Component to input element” related questions

How to pass ng-model from Angular 1.6 Component to input element

I am trying to create an Angular 1.6 component that is a wrapper for an input tag. I am creating a type ahead or autocomplete. I have to use ng-model in the html to create the component and that is

Show Detail

Pass parameter from Angular component to custom element

I am mergin an Angular app with an js (Lit element) app and I need to pass paramaters from the angular component to the custom element in the JS app. I cannot seem to pass the actual value, but onl...

Show Detail

Angular 2 @Input: Is it possible to pass data from a component to another child route component?

Based on the docs of Angular 2 you can pass data from a component to another quite easily with the @Input. So for example I can set the parent like this: import { Component } from '@angular/core';

Show Detail

how to pass value to input time tag using ng-model

Hi i am trying to pass value to a input time tag, from javascript - angular JS. Here is the following code. JS : $scope.from="09:00:00" $scope.from="17:00:00" HTML : From : &lt;input typ

Show Detail

How to pass a parameter from html page to angular custom element

I've created an angular custom element from an angular component that I want call from a normal html page. The component requires a parameter, which works when it's called as a component from withi...

Show Detail

Pass value from input component to button component in Angular application

I'm trying to figure how to pass value from one Angular component to another my-input.component and my-button.component: I'm trying to receive from my-input.component.ts and display @Input('name') ...

Show Detail

How to input an observable into an Angular web component

Please note that this question is NOT about regular Angular components. I'm specifically asking about a reusable custom element created with Angular also called an Angular web component. I have a

Show Detail

Pass @input property value from an angular component into component module

I am currently working on an Angular component where I would like to have a dynamic store name, for my ngrx/store(feature module). As a result, I have added an @Input() storeName: string; value to my

Show Detail

How to get an Element Reference from the Template Reference which is passed as an Input to a Angular Component

I need to capture the Input Element Reference in an Angular Component. We usually use @ViewChild (refer element from template) or @ContentChild (refer element from content injected) In my case, I p...

Show Detail

How to attach angular input to dynamic angular web element

We are trying to pass inputs to an angular web component. To make system robust, from api we will get two values Script URL Selector name ( below example component we are getting from db as well )...

Show Detail