Steps to trigger an AngularJS modal window when the URL contains a query string

I am in the process of creating a website and I would like to have a $uiModal open when there is a querystring in the URL. If there is no query string, then the modal should not open. Here is the code snippet:

myApp.controller('jobObjectiveController', ['$scope', '$http', '$uibModal', 'EmployeeObjectiveService', '$routeParams', function ($scope, $http, $uibModal, EmployeeObjectiveService, $routeParams) {

$scope.init = function () {
    $scope.AddButton = 'Save';
    $scope.isProcessing = false;
    $scope.UpdateButton = 'Update';
}

var id = $routeParams.id;


var items = [];
for (i = 0; i <10; i++) {
    items[i] = i;
}
$scope.test = items;

$scope.Objective = {
    Title: '',
    KPI: '',
    Target: '',
    Weight: '',
    Note:''
}

$scope.init();

//Column Selection 
EmployeeObjectiveService.GetObjectiveColumnList().then(function (response) { $scope.ColumnList = response.data });
EmployeeObjectiveService.GetObjectiveSeletedColumnList().then(function (response) { $scope.SelectedColumn = response.data });
EmployeeObjectiveService.GetObjectiveSeletCol().then(function (response) { $scope.selectCol = response.data});

//Change Events for Multiple dropdown
$scope.changeEvents = {
    onItemSelect: function (item) {
        changeColumnViewShow(item);
    },
    onItemDeselect: function (item) {
        changeColumnViewShow(item);
    }
};

function changeColumnViewShow(item) {
    if (item.id == 1) {
        $scope.selectCol.Code = !$scope.selectCol.Code;
    } else if (item.id == 2) {
        $scope.selectCol.Title = !$scope.selectCol.Title;
    } else if (item.id == 3) {
        $scope.selectCol.KPI = !$scope.selectCol.KPI;
    } else if (item.id == 4) {
        $scope.selectCol.Target = !$scope.selectCol.Target;
    } else if (item.id == 5) {
        $scope.selectCol.Weight = !$scope.selectCol.Weight;
    } else if (item.id == 6) {
        $scope.selectCol.Note = !$scope.selectCol.Note;
    } else if (item.id == 7) {
        $scope.selectCol.Status = !$scope.selectCol.Status;
    }
}

//Multiple Dropdown Config
$scope.dropConfig = {
    scrollable: true,
    scrollableHeight: '200px',
    showCheckAll: false,
    showUncheckAll: false
}

//Group by Pending and Approve
EmployeeObjectiveService.getGroupItem().then(function (response) { $scope.GroupBy = response.data });

//Open Modal for Add Objective
$scope.addObjective = function () {
    var modalInstance = $uibModal.open({
        templateUrl: '/View/Modal View/addObejective.html',
        controller:'jobObjectiveController',
        scope: $scope,
        size: 'lg'
    });
}

//Open Modal for View Objective
$scope.viewObjective = function (data) {
    var modalInstance = $uibModal.open({
        templateUrl: '/View/Modal View/ObejectiveModal.html',
        controller: 'jobObjectiveController',
        scope: $scope,
        size: 'lg',
    });
}

//Open Modal for Update Objective
$scope.updateObjective = function (data) {
    $scope.Object = {
        Title: 'xgdsg',
        KPI: 'sgsg',
        Target: 'sgsg',
        Weight: 'sgsgggggg',
        Note: data
    }
    var modalInstance = $uibModal.open({
        templateUrl: '/View/Modal View/updateObejectiveModal.html',
        controller: 'jobObjectiveController',
        scope: $scope,
        size: 'lg'
    });
}


//Add the Objective
$scope.AddObjective = function (data) {
    $scope.AddButton = 'Saving...';
    $scope.isProcessing = true;
    if (!data) {
        alert("Submitting...");
        $scope.init();
    }else
        $scope.init();

}

//Update Objective
$scope.UpdateObjective = function (data) {
    $scope.UpdateButton = 'Updating...';
    $scope.isProcessing = true;
    if (!data) {
        alert("Submitting...");
        $scope.init();
    } else
        $scope.init();

}

if (id != null) {
    $scope.viewObjective(id);
}

While testing the code with a querystring, the following errors are displayed:

TypeError: Cannot read property 'length' of undefined
at m.$scope.getButtonText (angularjs-dropdown-multiselect.min.js:1)
at fn (eval at compile (angular.min.js:1001), <anonymous>:4:230)
at angular.min.js:534
at m.$digest (angular.min.js:600)
at m.$apply (angular.min.js:610)
at l (angular.min.js:397)
at J (angular.min.js:417)
at XMLHttpRequest.t.onload (angular.min.js:420)

Answer №1

One must pay attention to the changes in the URL and verify if there is a query string present in order to trigger the opening of the modal:


$rootScope.$on("$routeChangeStart", function(args){})

Answer №2

To determine whether you are using route parameters, it is correct that you will receive an id. However, if you are sending query string parameters instead, then you should use this code:

// given URL http://example.com/#/some/path?foo=bar&baz=xoxo
var searchObject = $location.search();
// => {foo: 'bar', baz: 'xoxo'}

// set foo to 'yipee'
$location.search('foo', 'yipee');
// $location.search() => {foo: 'yipee', baz: 'xoxo'}

By following this method, you can retrieve the information and set a condition in the init method. This way, if there is a parameter id present, you can then proceed to call the open method of the modal.

For further details, please refer to Angular $location.

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

AngularJS Constants in TypeScript using CommonJS modules

Let's talk about a scenario where I need to select a filter object to build. The filters are stored in an array: app.constant("filters", () => <IFilterList>[ (value, label) => <IFilterObject>{ value: value, label: label } ]); i ...

ng-bind-html is functional, yet it is raising a TypeError

Below is the code snippet containing the ng-bind-html: <span ng-bind-html="text"></span> Here is the stack trace: angular.js:13236 TypeError: bindings.push is not a function at Function.$$addBindingInfo (http://localhost:9000/bower_component ...

Adding a new column for each array element within a jQuery foreach loop is a simple task that

I have a request where I am receiving three arrays in JSON format and I want to showcase each array in its own column. How can I accomplish this task? Below is the $.post function: $.post("/booking/times", { id: $("#user_id").val(), ...

Invoke a function in one module that resides in a different module

Having two angularjs files has presented me with a challenge. I've created some functions inside a controller and now I want to call these functions from another module and controller. Despite attempting to inject the module dependency into my caller ...

Rails: sending a controller object back to the AJAX requester

When I make an AJAX call to a Rails controller, retrieve a record, and then pass that record back to the AJAX function, I encounter a strange issue. Here is my AJAX request written in CoffeScript: jQuery -> $.ajax type: 'GET', url: ...

If the user inputs any text, Jquery will respond accordingly; otherwise,

I have a text field that is populated from a database, but if a user decides to change the value, I need to use that new value in a calculation. $('#ecost input.ecost').keyup(function(){ if (!isNaN(this.value) && this.value.length != ...

Updating a URL once AngularJS has finished processing it

Currently, I am utilizing Primo, an AngularJS app developed by Ex Libris. This application functions as a library catalog, enabling users to explore both physical and digital collections within the library. Despite not having direct access to the original ...

Implementing a universal (click) attribute for Angular 2 in CSS

When using Angular 1, it was as easy as following this syntax: [ngClick], [data-ng-click], [x-ng-click] { cursor: pointer; } This snippet ensured that any tags with the ng-click attribute displayed a pointer cursor. How can we achieve the same effect ...

How can I display the Facebook "feed" pop up in a jQuery dialog box?

Is there a method to display the Facebook feed pop-up (that posts to your wall) inside a jQuery dialog box? The code I currently have opens the dialog box and feed as separate pop-ups: <script> $(function() { $( "#dialog-modal" ).dialog({autoOpen: ...

Issue with jquery .val() function not functioning as expected

I am experimenting with the jQuery .val(value) function to set the value of an attribute using the code snippet below. <!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">< ...

Exploring the functionalities of jQuery within ajax-loaded content

My web page consists of the following code: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>test</title> <script src="jquery-2.1.1.min.js"></script& ...

The battle between $scope.$evalAsync and $scope.$applyAsync: Which one

Can anyone explain the distinction between $evalAsync and $applyAsync? From my understanding, using $evalAsync from a directive means that the expression will be evaluated before the browser renders. For instance, if I wanted to scroll to a specific posit ...

When attempting to implement sound on hover with an <a attribute containing an image>, the functionality is not functioning as expected

Is there a way to make a sound play only once when hovering over a list item that contains an image? Here is the HTML and JavaScript code I am using: function playclip() { var audio = document.getElementsByTagName("audio")[0]; audio.play(); } <ul ...

Is React the best solution for managing a lengthy list that requires constant data updates?

In order to display a long list with over 2000 entries that changes dynamically, I am utilizing react redux. Each second, at least one new row is added to the list data. My current approach involves mapping through the list data in the render method like t ...

jQuery locates all elements with a specific class after all other elements

I am working with multiple dynamically generated divs that share the same class. The amount of these divs can vary, so I do not have a fixed number in advance. My goal is to insert an additional div after the last one in the sequence. <div class="some ...

What is preventing me from choosing the complete table?

I am currently working on a method to export tabular data from an HTML page to Excel using DocRaptor. My approach involves using PHP to pass the necessary data through their API for conversion into an Excel Spreadsheet. However, I have encountered an issu ...

storing selected button ID's in an array with the help of AngularJS

I have 5 buttons with unique ids. Each id is a random name. My goal is to add these id names into an array called myClicks. I've attempted using "data.id" and "id" but neither has worked - the array remains empty. <!DOCTYPE html> <html ng- ...

AJAX is failing to refresh the page

I'm encountering a peculiar issue with my project. I need some guidance on why this occurs and how to resolve it. Within my project, I have a view function for editing/updating objects (function_edit). Additionally, I utilize AJAX to update a list of ...

Update the div element without needing to reload the entire page

Is there a way to reload a div tag without refreshing the entire page? I understand this question has been asked before, but I want to make sure I have a clear understanding. <p>click HERE</p> <div class="sample"> <?php functi ...

Utilizing JSON, HTML, and jQuery to create a dynamic cascading dropdown menu

Is there a way to dynamically populate a dropdown menu for states and cities using jQuery? I have a JSON file with the state-city mapping, how can I implement this? Here is an example of the JSON file: [ { "state":"Karnataka", "cities": ...