Encountering issues with Dependency Injection for ngMaterial

I'm in the process of creating an application that utilizes both dirPagination and AngularJS material. Unfortunately, I'm facing issues when trying to add the ngMaterial dependency to my application. While the angularUtils.directives.dirPagination dependency functions correctly, introducing ngMaterial causes my AngularJS app to crash.

Here's an example on Plunker (https://plnkr.co/edit/7N7i9TnMc77Nz6xlNhuk?p=preview) showcasing the problem. In the app.js file where I define the module, both dependencies are included. If the line is:

angular.module('myApp', ['angularUtils.directives.dirPagination', 'ngMaterial']);

The AngularJS app crashes and the page displays {{hello}}. However, if I modify the line in the controller to:

angular.module('myApp', ['angularUtils.directives.dirPagination']);

The page shows "Hello Plunker!" as expected since $scope.hello is set to "Hello Plunker!" in the controller.

Can you help me troubleshoot this issue?

Thank you for your assistance!

Answer №1

  1. When working with Angular, make sure to include angular.js before any other script that requires Angular. It's important to add script.js after angular.js

  2. Additionally, it's crucial to use the same versions of Angular modules such as angular-animate, angular-aria, angular-messages as the version of angular.js being used. In this case, you utilized the 1.6.4 version of angular.js.

        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-animate.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-aria.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-messages.min.js"></script>
    

To view the modifications I made in your plnkr, click on the following link: https://plnkr.co/edit/En0YaiaiqD1MPPOydAf8?p=preview

Furthermore, there were issues with cross-domain requests for bootstrap.min.css which can easily be resolved by using:

https://

cdn

Answer №2

The material.css script is missing and the order in which scripts are included is incorrect.

  <script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ccada2abb9a0adbee2a6bf8cfde2ffe2fc">[email protected]</a>" data-semver="1.3.0" src="https://code.angularjs.org/1.3.0/angular.js"></script>
  <link rel="stylesheet" href="//rawgit.com/angular/bower-material/master/angular-material.css" />
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-animate.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-aria.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-messages.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.0/angular-material.min.js"></script>
  <script src="script.js"></script>
  <script src="dirPagination.js"></script>

VIEW WORKING DEMO

Answer №3

This is a functional demonstration that encountered version conflicts and issues with the order of declaring the JS file.

<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/angular_material/1.0.0/angular-material.min.css">
    <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-animate.min.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-aria.min.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-messages.min.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/angular_material/1.0.0/angular-material.min.js"></script>
      <script data-require="jquery@*" data-semver="2.0.3" src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
    <script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4b2924243f383f392a3b0b78657a657a">[email protected]</a>" data-semver="3.1.1" src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
    
  </head>

  <body>
    <div data-ng-app="myApp">
      <div data-ng-controller="myCtrl">
        <h1>{{hello}}</h1>
        <md-toolbar class="md-warn">
         <div class="md-toolbar-tools">
            <h2 class="md-flex">HTML 5</h2>
         </div>
      </md-toolbar>
      </div>
    </div>
<script>
  var app = angular.module('myApp', ['ngMaterial']);
  app.controller('myCtrl', function($scope, $http) {
$scope.hello = "Hello Plunker!";
  });


  </script>
  </body>

</html>

Answer №4

One important point to note is that in order for angular-material.js to function properly, you must include both angular-animate.js and angular-aria.js before it in your code. Interestingly, it appears that angular-messages.js may not be necessary for this particular setup.

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

Dynamic filtering with Javascript

I am searching for inspiration on how to create a filter in the left sidebar that dynamically updates the page content when clicked, and if there are subcategories, displays them below the selected filter in the sidebar. I've discovered that AJAX is ...

Can you explain what the state represents in the $stateProvider?

I'm brand new to Ionic and AngularJS. I would greatly appreciate it if someone could share a tutorial with me that breaks down the syntax of $stateprovider. Specifically, I would like to understand what the .state element refers to and how it should b ...

Guide on retrieving JSON information through an AJAX request within an AngularJS Bootstrap modal

I am eager to implement the following functionality: a button that triggers a dialog/modal from Angular Bootstrap which showcases a loading indicator as the application retrieves JSON data from the server. Once the data is fetched, it should be displayed w ...

Determine the exact beginning and ending positions of a word when dividing it using Javascript

After creating a special function that breaks down a word based on spaces while keeping punctuation marks intact, I was able to achieve the desired result. function tokenizeUtterance( utterance ) { let spilittedUserText = utterance.toString().match( /[& ...

The retrieval of data using PHP, MYSQL, and AJAX is unsuccessful

A dropdown menu contains the names of months, represented by numbers from 1 to 12: When a month is selected, I want to retrieve data from a database to display relevant tournaments for that specific month. The SQL code has been tested and confirmed to be ...

The XMLHttpRequest() function throws NS_ERROR_FAILURE when sending requests to localhost using either an absolute or relative path

Encountering an error in Firefox 31 ESR: Error: NS_ERROR_FAILURE: Source file: http://localhost/Example/scripts/index.js Line: 18 Similar issue observed on Internet Explorer 11: SCRIPT5022: InvalidStateError The script used for AJAX function call i ...

What is the best approach for incorporating remote validation in a KendoUI grid with AngularJS and Web API integration?

Currently, I am facing a challenge of inserting a product into a SQL table. My approach involves using Kendo UI grid and AngularJS technologies. I am seeking guidance on how to properly validate whether the product already exists in the database before exe ...

I'm looking to integrate Jest in my React project with npm. How can I achieve

I've developed my application with create react app and npm. While reviewing the official documentation for jest at https://jestjs.io/docs/tutorial-react, I noticed that they only provide information on testing CRA apps using yarn. Does this suggest t ...

Setting up a service URL with parameters using a versatile approach

I am faced with a situation where I have over 200 service URLs that follow a specific format: serviceURL = DomainName + MethodName + Path; The DomainName and MethodNames can be configured, while the path may consist of elements such as Param1, Param2, an ...

Issue: Multiple resolutions detected in the future

After successfully running my angular app for the first time on the browser, I encountered errors when trying to refresh the page. The error messages were as follows: /.../node_modules/fibers/future.js:248 throw new Error('Future resolved mo ...

Hide Navbar when clicking on menu item

I'm struggling with finding a solution to this issue, as I am unsure of how to proceed. The problem I am facing is that when I click on an item, the router-view changes correctly, but the menu remains open. I would like it to close after a click. ...

Adjust the color of the paper in Material-UI

I'm in the process of creating a React project using the material-ui library. Currently, I am facing an issue with customizing the drawer component's background color. After some research, I learned that modifying the paper attribute of the drawe ...

Utilizing the Vuex/Redux store pattern to efficiently share a centralized source of data between parent and child components, allowing for customizable variations of the data as

Understanding the advantages of utilizing a store pattern and establishing a single source of truth for data shared across components in an application is essential. Making API calls in a store action that can be called by components, rather than making se ...

From traditional relational databases to MongoDB/Mongoose database design

Recently, I ventured into using mongoDB and mongoose for a new node.js application. Coming from a background of relational databases, I find it challenging to adjust to the mongoDB/noSQL approach, particularly concerning denormalization and the absence of ...

As we hover over a specific div, a secret div emerges and shifts its position

Looking to create a hidden div that appears and moves when hovering over another div. My current code is functional, but the hidden div does not move as the mouse moves on the div. This is likely due to the hover function being used. How can I address thi ...

Error in JavaScript goes undetected when utilizing asynchronous AJAX

Having recently started working with ajax and javascript, I find myself puzzled by the fact that my error is not getting caught when making an asynchronous ajax call. I did some research on a similar issue in this query (Catch statement does not catch thr ...

Is it advisable to combine/minimize JS/CSS that have already been minimized? If the answer is yes, what is

Our app currently relies on multiple JS library dependencies that have already been minified. We are considering consolidating them into a single file to streamline the downloading process for the browser. After exploring options like Google Closure Compi ...

The arrow toggle feature in the Angular Material MenuBar is experiencing issues with toggling properly

Currently, I am working with the Angular Material MenuBar and trying to incorporate an arrow toggle feature. The goal is to have the arrow point up when the menu is open and down when it is closed. I've been using ng-show to make the arrow toggle whe ...

issue occurring after inserting a new parameter

I encountered an issue with my test case after adding a new parameter tiger to the method swimming. Despite passing the new parameter tiger to my test case, it continues to break. Update: I am receiving an "undefined grid" error at this line. Any suggest ...

Must run the angular code in a sequential order

I need to run the code in a specific order; first the foreach loop should be executed, followed by a call to the getHistory() method. Your assistance is greatly appreciated. const execute = async()=>{ await this.currentContent.forEach(async ...