Exploring the differences between AngularJS directives and routeProvider

I'm encountering an issue with my directive. The $routeProvider is not getting triggered when the $location changes the URL path.

Here's the code snippet:

var appPath = location.pathname;
var bdaApp = angular.module('bdaApp', ['ui.bootstrap', 'ngResource', 'ngRoute', 
                                       'homeController', 'itemController', 'userController', 
                                       'eildController']);

    bdaApp.config(['$routeProvider', function($routeProvider) {

        $routeProvider
            .when('/', { templateUrl : 'layout/home', controller : 'HomeListController'})
            .when('/item', { templateUrl : 'layout/item', controller : 'ItemListController'})
            .when('/user', { templateUrl : 'layout/user', controller : 'UserListController'})

            .when('/eild/consultar', { templateUrl : 'eild/layout', controller : 'EildListController'})
            .when('/eild/detalhes/:bilhetePedido', {templateUrl : 'eild/detalhes', controller : 'EildDetailController'});

    }]);

    bdaApp.directive('myDirective', ['$location', function($location) {
        return {
            restrict: 'A',
            template:
                    '<ul class="nav navbar-nav">' +
                    '   <li ng-repeat="p in parents" class="dropdown">' +
                    '       <a ng-click="go(p.path)" class="dropdown-toggle">' +
                    '           {{p.nome}}' +
                    '           <b ng-show="p.menus.length > 0" class="caret"></b>' +
                    '       </a>' +
                    '       <ul ng-show="p.menus.length > 0" class="dropdown-menu">' +
                    '           <li ng-repeat="s in p.menus">' +
                    '               <a ng-click="go(s.path)">{{s.nome}}</a>' +
                    '           </li>' +
                    '       </ul>' +
                    '   </li>' +
                    '</ul>',
            controller: ['$scope', '$resource', function($scope, $resource) {

                $scope.getItems = function() {
                    $resource(appPath + 'menus/teste').query(function(data) {
                        $scope.parents = data;
                    });
                };
            }],
            link: function(scope, iElement, iAttrs) {
                scope.getItems();
                scope.$watch('parents', function(newVal) {});

                scope.go = function(path) {
                    if (path != null) {
                        $location.path(path);
                    }
                };
                scope.$watch('go', function(newVal) {});
            }
        };
    }]);

and here's my HTML:

<div>
    <div my-directive items="items"></div>
</div>

Whenever I click on a link, the "go" function gets called and changes the URL to "

http://localhost:8080/bda-web/#/item
", but the page doesn't change. This leads me to believe that the routeProvider is not being triggered by my directive.

I also attempted to move my function externally like so:

bdaApp.controller('MenuController', ['$scope', '$location', function($scope, $location) {
    $scope.go = function(path) {
        if (path != null) {
            alert(path);
            $location.path(path);
        }
    };
}]);

However, I was unable to make this work.

Any suggestions would be greatly appreciated!

Thank you

Answer №1

I found a solution by utilizing <a href> in place of <a ng-click>

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

The Ajax request fails to send the form files

I'm encountering an issue where a form isn't passing one variable. Here is my table structure: Schema::create('projects', function(Blueprint $table) { $table->increments('id'); $table->string(&apos ...

Using htaccess, we can redirect to the dist folder and enable AngularJS html5mode

Working on an AngularJS application, I am faced with the task of enabling html5mode to remove the hash /#/ from the URL. The minified version of the app can be found in the dist folder. QUERY: My objective is to redirect traffic to the dist folder while a ...

React Native: useEffect not triggering upon navigation to a screen that is already active

When I click on a notification in my React Native app, it navigates me to a chat screen. In that chat screen, there is a useEffect function that fetches the chat messages. The issue arises when the chat screen was the last screen opened before closing the ...

Steps for importing a Java project into Eclipse and configuring it to run on a Tomcat server

I need guidance on how to import an external project into a Tomcat server in order to view the output in a browser. The project is an online banking system developed using JSP, AJAX, and JavaScript. ...

What is the method for cancelling an HTTP request in the prototype JavaScript library?

Below is the code snippet: var pendingRequest = new Ajax.Request(myUrl, { method: 'post', postBody: soapMsg, contentType: "text/xml", onSuccess: function(transport) { ...

Select a single option from the group to include in the array

I'm currently developing a new soccer betting application. My goal is to allow users to choose the result of a match - whether it's a win, loss, or draw - and then save that selection in a list of chosen bets. https://i.stack.imgur.com/hO3uV.png ...

Storing data seamlessly within a controller using AngularJS on the same page

Can someone please assist me with saving data into an array and displaying it in a table within a form? <div ng-app="farmerapp" ng-controller="RegController"> @Html.TextBoxFor(x => x._Cetificate.CertificateName,new { data_ng_model = "_Ce ...

Is there a chance of a race condition occurring during file uploads when processed individually through AJAX with PHP?

I have created a form for uploading multiple files. <form id="myuploadform" enctype="multipart/form-data"> <input id="uploadedFiles" name="uploadedFiles" type="file" class="form-control&qu ...

The error message displayed reads as follows: "topojson has encountered a TypeError: Unable to access the property 'feature' as

Context A problem has arisen with JavaScript when trying to use the topojson.feature(topology, object) function. It seems that this issue appeared after moving from TopoJSON version 1.6.26 to version 2.x, although the functionality remains similar. The p ...

Change the value of ng-option based on the selected value of another select option

I need to enhance a select statement based on another selection I have checked out examples online and other posts here on Stack Overflow, but they are missing something. What I am after is this: <select ng-model="topSelect" ng-change="updateBottomSe ...

What is the best way to launch a Popup window that spans from the top to the bottom of the screen?

I'm attempting to create a popup window that stretches from the top of the screen to the "applications" bar in Windows. Here's the code I'm using: function windowOpener(windowHeight, windowWidth, windowName, windowUri) { var windowHeight = ...

MVC error encountered when attempting to pass the parameter '&' results in failure

Encountered an error in MVC while passing '&' as a parameter, which fails to display the desired outcome. How can I address this issue with the following example? public IActionResult Create(int? idTrabalhador, [FromQuery]string GridState{} ...

How to utilize regular expressions in JavaScript without the need to instantiate a regex object

Here is a code snippet that checks a URL for a specific pattern: var url = "https://www.google.co.uk/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=regex%20javascript"; var patt = new RegExp('https?:\/\/[^/]+\.google ...

"Code snippets customized for React are not functioning properly in the javascriptreact.json file within Visual Studio Code

I'm looking to incorporate some customized React.js code snippets into my Visual Studio Code setup. I am aware of the two files in VSCode that handle this - javascript.json and javascriptreact.json. Although the snippets work well in javascript.json, ...

show the attributes of an item contained within an array of objects

Hey there! I'm facing an issue with displaying the content of an object on my HTML page using React. So, here's what's happening: I can access the frontend properties in my object array, but when I try to loop through each element and displa ...

Having trouble with my Express.js app, the static files are not

Code Snippet using Express exports.index = function(req, res){ var info = {title: 'Index'}; res.render('index' , info); }; exports.about = function(req, res){ var info = {title: 'about'}; res.render('abo ...

A single integer variable that is shared across all sessions in node.js

Is there a way to create an integer variable that is shared among all client sessions in node.js and can be accessed in client HTML? I need this variable to be displayed in the HTML page and have the ability to update it within the page, with the new val ...

Attempting to utilize the ngSwitch directive within an Angular application

I have been experimenting with this code snippet in Angular, but unfortunately, it is not functioning as expected. As a newcomer to Angular, I am struggling to solve this issue. My objective is simply to display a specific element based on a step number ...

What is the most effective method to retrieve the current browser URL in Angular 2 with TypeScript?

Is there a way for me to retrieve the current URL from the browser within my Angular 2 application? Usually in JavaScript, we would use the window object for this task. Can anyone guide me on how to achieve this in Angular 2 using TypeScript? Appreciate a ...

`Inability of ngmodel to bind integer values on select element`

From the service, I will be receiving an integer as the selected value and need to send it back as an integer. Please refer to the code in this Plnkr link. <!-- Working --> <div ng-init="selectedvalue = '3'"> <select ng-model= ...