Error: The ngModelProvider provider is not recognized

Attempting to incorporate ngModel into an Angular directive results in the following error:

Error: [$injector:unpr] Unknown provider: ngModelProvider <- ngModel
http://errors.angularjs.org/1.2.18/$injector/unpr?p0=ngModelProvider%20%3C-%20ngModel
    at http://localhost:2013/Scripts/angular.js:78:12
    at http://localhost:2013/Scripts/angular.js:3741:19
    at Object.getService [as get] (http://localhost:2013/Scripts/angular.js:3869:39)
    at http://localhost:2013/Scripts/angular.js:3746:45
    at getService (http://localhost:2013/Scripts/angular.js:3869:39)
    at invoke (http://localhost:2013/Scripts/angular.js:3896:13)
    at Object.instantiate (http://localhost:2013/Scripts/angular.js:3917:23)
    at $get (http://localhost:2013/Scripts/angular.js:7201:28)
    at http://localhost:2013/Scripts/angular.js:6592:34
    at forEach (http://localhost:2013/Scripts/angular.js:327:20) angular.js:9937
(anonymous function) angular.js:9937
$get angular.js:7283
$get.Scope.$digest angular.js:12414
$get.Scope.$apply angular.js:12660
done angular.js:8272
completeRequest angular.js:8477
xhr.onreadystatechange

The directive code is as follows:

module.directive("myDatePicker", function () {
    return {
        restrict: "A",
        template: '<p class="input-group" title="{{title}}">' +
                            '<input type="text" class="form-control" data-datepicker-popup="{{dateFormat}}" ng-model="selectedDate" data-is-open="isOpen" data-datepicker-options="dateOptions" ng-required="true" data-close-text="{{closeText}}" />' +
                            '<span class="input-group-btn">' +
                                '<button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>' +
                            '</span>' +
                        '</p>',
        replace: true,
        require: '?ngModel',
        //priority: 0,
        scope: {
            title: "@",
            selectedDate: "=ngModel",
            onChange: "&",
            dateFormat: "="
        },
        compile: function (tElement, tAttrs, transclude) {
            // Correct ngModel for isolate scope
            if (tAttrs.ngModel) {
                tAttrs.$set('selectedDate', tAttrs.ngModel, false);
                tAttrs.$set('ngModel', 'selectedDate', false);
            }

            return {
                post: function (scope, iElement, iAttrs, controller) {
                    // Render
                    return controller.$render = function () {
                        if (!controller.$viewValue) {
                            return;
                        }
                        angular.extend(scope, controller.$viewValue);
                    };
                }
            };
        },
        priority: 100,
        link: function (scope, elem, attr) {
        },
        controller: function ($scope, global, ngModel) {
            $scope.isOpen = false;
            $scope.closeText = 'Close';
            $scope.dateOptions = {};

            $scope.open = function ($event) {
                $event.preventDefault();
                $event.stopPropagation();
                $scope.isOpen = true;
            };

            $scope.$watch(function () {
                return $scope.selectedDate;
            },
                function (newValue, oldValue) {
                    ngModel.$setValidity('required', newValue == true);
                    $scope.onChange({ newDate: newValue, oldDate: oldValue });
                }, true);
        }
    };
});

Here is the corresponding HTML snippet:

<input data-my-date-picker
                           id="EventDatePicker" data-name="EventDatePicker"
                           data-date-format="dateFormat"
                            ng-model="EventDetails.Date"
                           ng-required="true"
                           data-title="Date" />

Why is the injection not resolving? Attempts have been made like:

module.directive("myDatePicker", ['ngModel', function () {

...but this did not resolve the issue.

The removal of priority also yielded no solution.

Could it be related to the loading order of directives?

What might be missing here?


After implementing sma's suggestion, the following change was applied:

        require: ['^ngModel'/*, '^isDate'*/],
        scope: {
            title: "@?",
            name: "@?"
        },
        link: function (scope, elem, attr, ngModel/*, isDate*/) {
        ...

This appears to have partially resolved the issue.

The inability to inject this into the controller remains a mystery. :(

There must be a solution out there, waiting to be discovered...

In a puzzling turn of events, other directives like isDate are still not being injected (despite being defined before the current directive).

This situation is perplexing.

Answer №1

It seems there may be some errors in your controller function's arguments. The proper way to define a controller function is as follows:

controller : function ($scope, $element) {
   ...    
}

If you need to inject ngModel as a dependency, you should use the link function instead:

link : function(scope, element, attrs, ngModelController) {
   ...
}

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

Confirming the username's accuracy through an external API as part of the registration procedure

My latest project involves creating a web application specifically for Fortnite players looking to connect with other gamers. The concept is simple: users can register, log in, post, and comment within the platform. I have successfully designed the fronten ...

Failure to validate two dates, even when they are both in date format within AngularJS

Although it may seem silly to ask, I am confused as to why this is not working. Even though all the values appear fine in debug mode. My goal is to display an error if the productionStartFrom date is before the current date. Controller scope.currentDate ...

Using AJAX to dynamically populate PHP form inputs from HTML

I'm trying to create a simple HTML form where users can input their information and have it sent to a PHP file using JavaScript with AJAX. However, I'm encountering an issue where the values from the form are not being included in the email that ...

What is the best way to transfer a variable to an isolated scope function?

I have set up a directive as shown below - <div data-my-param-control data-save-me="saveMe()"></div> Within the directive controller, I have connected the saveMe() function from the controller to an isolated scope like so - function MyParam ...

Unable to produce audio from files

Attempting to incorporate sound files into my project using https://github.com/joshwcomeau/redux-sounds but encountering difficulties in getting it to function. Below is the code snippet I utilized for setup. Unsure if webpack is loading the files correctl ...

Angularjs changes the "&" character to "&"

Here is the AngularJS code I am using: $scope.getEventSeconds = function(){ $http.get('myfile.php', { params: { 'action': 'get_datas' } }).success(function(data){ $scope.list = data; $scop ...

Looking to detect and notify the presence of duplicate values within an array

I am encountering an issue where I can view an array in the console, but cannot check if it contains duplicate values. $('.tab2field').each(function () { PackageName.push($('.span2', this).val() ); PackageCount.push($(&apo ...

I currently have an array of strings and wish to print only the lines that include a specific substring

Here i want to showcase lines that contain the following strings: Object.< anonymous > These are multiple lines: Discover those lines that have the substring Object . < anonymous > Error: ER_ACCESS_DENIED_ERROR: Access denied for user ' ...

The video rendered with fluent-ffmpeg appears to have an elongated image

I am working on combining an mp3 audio file with a jpg image file to create a new mp4 video. Although I have a functional fluent-ffmpeg command that accomplishes this task, there is an issue where the image gets stretched in the final output video, especia ...

Implementing JSON parsing in an ESP32 application using AJAX script

Currently, I am engrossed in a project that involves utilizing ESP32. I'm obtaining data from various sensors and transmitting it to a webpage hosted on the same board. After doing some research online, I learned that it's considered "better" to ...

Guide to Making Colorful Text Inside Bootstrap 5 Tooltip

When trying to include the double quote character " in a custom HTML tooltip, it appears to cause the tooltip to malfunction. This results in the browser mistakenly treating the tooltip title as completed and interpreting a portion of the title as ext ...

Ajax function not returning expected value but instead printing echo

My task involves updating a span element with data from an update PHP function that executes mySQL queries, triggered by a button click. To achieve this, I utilized jQuery/Ajax combined with a separate function in another file. I learned that PHP log mess ...

Tips for incorporating a spinner during content loading within AngularJS

When the user clicks on the "Search" button, content will load and the button label will change to "Searching" with a spinner shown while the content is loading. Once the content has loaded (Promise resolved), the button label will revert back to "Search" ...

Error: The Vue Class-Based module failed to parse due to an unexpected character '@'

When I run the command nmp run serve on my Vue project, I encounter two errors. I am following a tutorial that uses class-based Vue, but I am facing this error with all my imported Vue files. As a newcomer to Vue, I am puzzled as to why this error is occur ...

What is the best way to display the names of students whose grade exceeds 70% using JavaScript?

I am currently utilizing the json-rule-engine library, which can be found at https://www.npmjs.com/package/json-rules-engine. In my scenario, I have a list of students with their names and corresponding percentages. Additionally, there is a specific busine ...

Using AngularJS to access form field ids that are generated dynamically

I am dynamically generating form fields using ng-repeat and everything is functioning correctly. However, I now want to incorporate an angular datepicker component that is based on a directive. The issue I am facing is that it only seems to work with stat ...

What's causing the error "is not a function" to appear?

I am currently facing an issue while attempting to develop an angular service. Here is the code snippet for the service: var app = angular.module('plunker', []); // Filter Service that returns records with crdamt positive: app.factory('Fil ...

Issue persists with `overflow:auto` even when height is defined

As I was creating a website, I designed an exception holder on the side to capture every error thrown. To prevent the webpage from expanding endlessly in case of multiple errors, I am working on making it scroll instead. My approach involves using CSS an ...

encountering an error when attempting to send an HTTP POST request and

Trying to pass user information using http angularjs with PHP backend code. As a beginner, I have been searching for a solution to this issue for the past 2 days, trying various methods with no success. It seems like a simple problem, but I can't see ...

preserving a photo that has been edited using caman.js

Hey everyone, I've been working on this for a few days and I could really use some help. I used camanjs to manipulate an image and then saved it to disk using canvas.toblob(). Here's the code: Caman("#theCanvas", "images/1.jpg", function () { ...