JavaScript Use ngModel on a custom directive with an embedded form, with working validation? ngmodel custom directive,angularjs ng model custom directive,ng-model

I have a commonly reused set of form inputs that are reused throughout my application, so I am trying to encapsulate them in a custom directive. I want to set an ngModel on my directive and have that split up to be editable in several different inputs (some of them are directives themselves) within the main directive.

At the same time, I need the form validation results to be passed up the chain to a parent form so that I can display appropriate messages and styles.

What is the simplest and most idiomatic way to implement this?

These (simplified) templates should give you an example of what I'm going for...

OuterTemplate.html

<form name="outerForm">
  <my-directive
    ng-model="ctrl.myComplexModel"
    name="myDirectiveInstance"
    custom-required="ctrl.EnableValidateOne"
    toggle-another-validation="ctrl.EnableValidateTwo">
  </my-directive>
  <div ng-messages="outerForm.myDirectiveInstance.$error">
    <ng-message when="customRequired">This is required.</ng-message>
    <ng-message when="anotherValidation">This is required.</ng-message>
    <ng-message when="innerValidationOne">Something wrong with field 1.</ng-message>
    <ng-message when="innerValidationTwo">Something wrong with field 2.</ng-message>
    <ng-message when="innerValidationThree">Something wrong with field 3.</ng-message>
    <!-- etc... -->
  </div>
</form>

myDirectiveTemplate.html

<div ng-form="myDirectiveForm">
  <div ng-class="{'has-error': myDirectiveForm.fieldOne.$invalid}">
    <ui-select
      ng-model="model.fieldOne"
      name="fieldOne"
      required>
    </ui-select>
  </div>
  <div ng-class="{'has-error': myDirectiveForm.fieldTwo.$invalid}">
    <input
      type="number"
      ng-model="model.fieldTwo"
      name="fieldTwo"
      ng-pattern="directiveCtrl.someRegEx"
      ng-required="directiveCtrl.fieldTwoIsRequired">
  </div>
  <!-- etc... -->
</div>

At the moment, both myDirectiveForm and myDirectiveInstance are publishing themselves as properties of the outerForm FormController. I hoping to make this directive a black box, so the fact that myDirectiveForm is attaching directly to outerForm bothers me and seems to indicate that I'm doing something wrong.

Here's what my directive definition looks like right now.

myDirective.js

app.directive('myDirective', function() {
  return {
    restrict: 'E',
    template: 'myDirectiveTemplate.html',
    controller: 'MyDirectiveCtrl',
    scope: {
      model: '=ngModel',
      customRequired: '=?',
      toggleAnotherValidation: '=?'
    },
    require: 'ngModel',
    link: function(scope, iElem, iAttrs, ngModelController) {

      // Black-box the internal validators

      // Custom validator to avoid conflicts with ngRequired
      ngModelController.$validators.customRequired = function(modelValue, viewValue) {
        if(!scope.customRequired)
          return true;

        // On first digest the field isn't registered on the form controller yet
        if(angular.isUndefined(scope.myDirectiveForm.fieldOne))
          return true;

        return !scope.myDirectiveForm.fieldOne.$error.required;
      };

      ngModelController.$validators.anotherValidation = function(modelValue, viewValue) {
        if(!scope.anotherValidation)
          return true;

        return scope.passesBusinessRule();
      };

      ngModelController.$validators.innerValidationOne = function(modelValue, viewValue) {
        if(!scope.anotherValidation)
          return true;

        if(angular.isUndefined(scope.myDirectiveForm.fieldTwo))
          return true;

        return !scope.myDirectiveForm.fieldTwo.$error.pattern;
      };

      /* etc... */

      // Deep-watching model so that validations will trigger on updates of properties
      scope.$watch('model', function() {
        ngModelController.$validate();
      }, true);
    }
  };
});
Answer:1

this is how I understand the directives. In this case both ng-model and myDirective are directives. It's not clear to me that if you are doing a

1) wrapper on ng-model OR 2) custom directive

Because if you want to do custom directive, you can just pass in the data, ex.

{ scope: { data: '=' }

And if you want to do a wrapper, you probably shouldn't pass in the other properties related to ngModel, which means you can still pass in the data

ctrl.myComplexModel, btw. model object can not be assigned to ng-model, because ng-model does not hold the object, it just holds the data.

NOTE: actually i found this post, AngularJS - Create a directive that uses ng-model

and apparently, you can pass in model, https://docs.angularjs.org/api/ng/type/ngModel.NgModelController

Anyway, it's just too complicated for me :) If you want to make a wrapper, the pattern seems to me

  1. pass in the data
  2. "has a" object

But apparently you might be doing "is a" object.

Answer:2

_renderItem is not executing at all, tried with console.log too no messages printed. Tried with 'autocomplete', 'ui-autocomplete', 'Autocomplete' attributes no hope. In addition I could not ...

_renderItem is not executing at all, tried with console.log too no messages printed. Tried with 'autocomplete', 'ui-autocomplete', 'Autocomplete' attributes no hope. In addition I could not ...

  1. jquery autocomplete _renderitem not working
  2. jquery autocomplete _renderitem undefined
  3. jquery autocomplete _renderitem example
  4. jquery autocomplete _renderitem
  5. jquery autocomplete _renderitem multiple
  6. jquery autocomplete _renderitem select
  7. jquery autocomplete _renderitem table
  8. jquery ui autocomplete _renderitem
  9. jquery ui autocomplete _renderitem example
  10. jquery ui autocomplete _renderitem not working
  11. jquery autocomplete override renderitem
  12. jquery autocomplete ajax _renderitem
  13. jquery autocomplete custom renderitem
  14. jquery ui autocomplete _renderitem highlight
  15. jquery autocomplete data _renderitem
  16. jquery ui autocomplete custom renderitem
  17. jquery ui autocomplete override renderitem
  18. jquery autocomplete custom rendered item
  19. jquery autocomplete renderitem
  20. jquery ui autocomplete renderitem

What does this expression mean in Vuejs? :class="{ expr1: expr2}" It appears be a truth expression, but is there a binary truth operator in Javascript?

What does this expression mean in Vuejs? :class="{ expr1: expr2}" It appears be a truth expression, but is there a binary truth operator in Javascript?

I have in the below parameters for all ajax calls in the <head> of my document. (I need this for an iOS ajax bug at https://stackoverflow.com/a/12856562/627473 $.ajaxSetup ({ cache: ...

I have in the below parameters for all ajax calls in the <head> of my document. (I need this for an iOS ajax bug at https://stackoverflow.com/a/12856562/627473 $.ajaxSetup ({ cache: ...

  1. jquery ajaxsetup headers
  2. jquery ajaxsetup headers authorization
  3. jquery ajax headers
  4. jquery ajax headers authorization
  5. jquery ajax headers access-control-allow-origin
  6. jquery ajax headers authorization bearer
  7. jquery ajax headers cors
  8. jquery ajax headers content type
  9. jquery ajax headers not working
  10. jquery ajax headers basic authentication
  11. jquery ajax headers post
  12. jquery ajax headers cookie
  13. jquery ajax headers json
  14. jquery ajax headers accept
  15. jquery ajax headers cross domain
  16. jquery ajax headers not added
  17. jquery ajax headers get
  18. jquery ajax headers origin
  19. jquery ajax headers not being set
  20. jquery ajax headers response

What do we call this type of argument passing mul(1)(2)(3) how to solve this and how to solve such scenario in case of n arguments passed like this. I want to understand how this concept works.

What do we call this type of argument passing mul(1)(2)(3) how to solve this and how to solve such scenario in case of n arguments passed like this. I want to understand how this concept works.

  1. what call this symbol
  2. what call this sign
  3. what this call regarding
  4. what's call the midwife about
  5. what call it means
  6. what call the wife
  7. what's call it a day
  8. what call it off means
  9. what call the police
  10. what's call it a night
  11. what call the person
  12. what call the situation
  13. what's call it quits
  14. what call it out
  15. what call the color
  16. what call the dance
  17. what call the process
  18. what call the numbers
  19. what we call this symbol
  20. what we call this symbol in english