What is the best way to designate my field as $dirty within my unique directive implementation?

I have created a custom dropdown/select directive to replace the default select boxes within my form. I also have some buttons that are set to disable while the form remains in its pristine state.

app.directive('dropdown', function ($timeout) {
    return {
        restrict: 'E',
        template: '<div class="btn-group" dropdown>' +
             '<button id="dropdownDirective" class="btn dropdown-toggle" dropdown-toggle>' + 
                '{{ items[ngModel].name }}' +
                '<span class="caret"></span>' +
            '</button>' +
            '<ul class="dropdown-menu" role ="menu" aria-label="dLabel">' +
                '<li ng-repeat="item in items">'+
                    '<a href="#" ng-bind="item.name" ng-click = "select(item)" >< / a >' +
                '</li>'+
            '</ul>'+
            '</div>',
        scope: {
            ngModel: '=', // selected option
            items: '=',   // options
        },
        link: function (scope, element, attrs) {
            scope.select = function (item) {
                scope.ngModel = item.id;
            };           
        }
    };
}); 

In HTML:

<dropdown id="customSelect"
    ng-model="ahs.custom.modalId"
    items="es.custom.data">
    </dropdown>

Although I have implemented this directive, the $pristine property is not being triggered as expected. Could someone please advise me on what could be going wrong?

Answer №1

In order to properly handle form-related features in your directive, you will need to include the following implementation:

https://docs.angularjs.org/api/ng/type/ngModel.NgModelController

  app.directive('dropdown', function ($timeout) {
      return {
          require: "ngModel",
          restrict: 'E',
          template: [...]
          scope: {
              items: '=',   // options
          },
          link: function (scope, element, attrs, ngModelController) {
              ngModelController.$setViewValue(...); 
              ngModelController.$setPristine(...);
              etc.
          }
      };
  }); 

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Discover the step-by-step process of combining an array with JSON to create the desired outcome

I am working with a JSON array that looks like this: var row={ shopId: 3, shopName: '1', address: 'abc', contactNumber: 1234 } Alongside, I have another array: var data= [ { imageId: 1, shopId: 3, imageUrl: 'aaa' }, ...

Error: The function setDarkTheme is not defined

Every time I click on the Dark button to apply dark theme on my page, I encounter an error. It seems like the error lies in the onClick function of the button and I can't seem to figure it out. Error message App.js import { useState } from 'rea ...

What is the cost associated with using the require() function in an Express.js application?

I have a web application built with Express.js that serves one of my domains. The structure of the app.js file is as follows: var express = require('express'); var app = express(); // and so on… To incorporate one of my custom functions in t ...

The combination of WASM and Node.js encounters an issue when attempting to utilize 'import.meta' outside of a module

I successfully compiled the FastText C++ module to a wasm module using the provided make file, with the following flags: EMCXX = em++ EMCXXFLAGS = --bind --std=c++11 -s WASM=1 -s ALLOW_MEMORY_GROWTH=1 -s "EXTRA_EXPORTED_RUNTIME_METHODS=['addOnPos ...

Using React for form validation

I'm facing a challenge while trying to develop a user registration form, especially when it comes to displaying form validation errors. Issues: 1) The input fails to post (via axios) to the database upon submission for inputs without errors. 2) The e ...

Is it possible to condense my angular-ui-router stateProvider definition?

While Angular-UI-Router is a great tool, I find that my code for defining the stateProvider is getting repetitive. For example: .state('home', { url: "/home", templateUrl: "home.html" }) .state('products', { url: "/products", t ...

What is the best way to automate sending emails for every error that arises in Node.js?

In the scenario where my node.js application is up and running, I am looking for a way to handle all types of errors (not just web errors) and have a function that can send an email notification when an error occurs. Essentially, before the error gets wr ...

Setting up instagram-node-lib for managing subscriptions

I am currently working on implementing real-time updates for a specific hashtag and displaying the image on the screen. However, I am facing issues setting up my node.js server using the instagram-node-lib module. Even after running the file (node server.j ...

The property 'matMenuTrigger' cannot be attached to 'a' because it is not recognized

Trying to implement matMenuTrigger but encountering an error saying "Can't bind to 'matMenuTrigger' since it isn't a known property of 'a'". Any assistance would be greatly appreciated. ...

Guide to transferring a JavaScript/Ajax variable to a PHP webpage with the help of AJAX

function initiateIperfSpeedRequest(speedVar, actualIp) { var xmlhttp = getAjaxObject(); xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { handleIperfResponse(xmlhttp.respo ...

The POST request is not functioning. An error has occurred: Unable to retrieve response data - no content is available for the preflight request

Having trouble making a post request from my client side to the back-end. Even with the new movie hardcoded, it keeps returning an error stating that there was a failure in loading the response data. Below is the code for the front-end: //Fetch request t ...

What is the reason behind Next.js inability to import files within a directory?

Error: Module not found - Unable to locate 'components/layout' in nextjs-blog/pages/posts > 1 | import Layout from 'components/layout' 2 | import Link from 'next/link' 3 | import Head from 'next/head' I' ...

Utilizing NPM configuration variables across different operating systems (Windows and Linux): A comprehensive guide

When you use the syntax npm_package_config_<variable>, its usage varies depending on the operating system. In package.json (for Linux & Windows) config: { foo: "bar" } Then for usage: On Linux node app.js --arg=$npm_package_config_foo On Wi ...

unable to apply angular measurement inside quotation marks

What is the reason for this issue: <ul class="dropdown-menu"> <li ng-repeat="choice in dropDownItems"> <a class="btn" ng-click="mnuClick('{{choice}}')">{{choice}}</a> </li> </ul> However, this code wo ...

Close the parent electron window while keeping the child window open

I am currently working on a project where I need to create an electron app that displays a splash screen initially and then opens a new window before closing the splash screen. However, despite my efforts, I am facing challenges in achieving this functio ...

Javascript loop woes

I have a number array ranging from 1 to 20. My goal is to create a loop that extracts every nth element from the array, starting with (1, 6, 11, 16). After the initial loop, it should then extract every 5th element, starting from 2 (2, 7, 12, 17). My atte ...

Directive for creating organizational charts with Angularjs

Are there any specific AngularJS directives available for creating organizational charts using wesnolte/jOrgChart or similar chart libraries? ...

With Vite, Vue.js can generate 400 requests from a single function invocation

Is it possible that there is a bug with Vite.js or is this behavior normal? I noticed that when using date-fns in Vite.js, it's making 400 requests. It seems like just one function call is resulting in 400 ajax requests being triggered by date-fns. A ...

exploring the contrast of css versus javascript selectors

Could you please explain the contrast between div#name and #name? Is there a significant difference when using class or id to position an element? Thank you for your help. ...

Exploring Local Gems with Google Places API in Ionic 2

I recently integrated the Google Maps API into my app and now I am looking to incorporate Google Places as well. I have managed to successfully implement Geolocation, but I am facing difficulties when trying to perform a nearby search for "supermarkets" in ...