Utilizing a function to call a controller and passing a URL-encoded parameter

I have developed a CRUD interface for a Rest service where I can currently retrieve a list of teams in the system.

My goal is to display details for each team when the show link is clicked. Currently, clicking on the link triggers a function (which has not been implemented yet) that should load the details into the ng-view. The function needs to pass a parameter to the ViewTeamController, which will then call the Rest service and store the result in a $scope variable.

I am uncertain about how to call the ViewTeamController, potentially using a URL-encoded parameter from the showTeam() function. Moreover, I would like to understand how to read the URL-encoded parameter within the ViewTeamController.

Thank you in advance for your assistance.

<!doctype html>
<html>

<head>
    <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script>
    <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular-route.min.js"></script>

    <script>
        var teamApp = angular.module("teamApp", ['ngRoute']);

        teamApp.controller('teamController', function($scope, $http) {

            $http
                    .get('/teams')
                    .success(function(response) {
                        $scope.teams = response;
                    }
                    );
        });

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

            when('/addTeam', {
                templateUrl: 'addTeam.htm',
                controller: 'AddTeamController'
            }).

            when('/viewTeam', {
                templateUrl: 'viewTeam.htm',
                controller: 'ViewTeamController'
            }).

            otherwise({
                redirectTo: '/addTeam'
            });
        }]);

        mainApp.controller('AddTeamController', function($scope) {

        });

        mainApp.controller('ViewTeamController', function($scope) {

        });

    </script>
</head>

<body>

<div ng-app = "teamApp" ng-controller="teamController">

    <a ng-click="newTeam()">new</a>

    <div ng-repeat="team in teams" >
        Name: {{team.name}}
        <br />
        Description: {{team.description}}
        <br />
        <a ng-click="showTeam(team.id)">show</a>
    </div>

    <div ng-view></div>

    <script type = "text/ng-template" id = "addTeam.htm">
        <h2> Add Team </h2>
        To be implemented later.
    </script>

    <script type = "text/ng-template" id = "viewTeam.htm">
        Name: {{team.name}}
        Description: {{team.description}}
    </script>
</div>
</body>
</html>

Answer №1

To access route parameters in your controller functions, you can use the AngularJS $routeParams service as shown below:

 mainApp.controller('TeamViewController', function($scope, $routeParams) {
              $scope.param = $routeParams.param;  // stores encoded parameters
        });

If you pass parameters in your URL like this,

/teamView/23535t4645645g4t4

Then $scope.param will contain 23535t4645645g4t4

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

Distinguishing Between Model and View State Management in AngularJS

As I work on a complex single page application in Angular, I find myself needing to manage two types of data. First, there is the Model data which pertains to information retrieved from and sent to the backend API server. Second, there is ViewState data wh ...

Difficulty activating JavaScript function - unsure of proper implementation

I'm having trouble with calling a function instead of an alert in my code. I tried using FunctionsName(); and removing the alert(''); but it doesn't seem to be working for me :( Could someone please take a look at the following code an ...

Maximizing the potential of input with jQuery

Having trouble retrieving the value from an input and passing it through a script to sign up a user. The alert I have in place to test it is not showing any value. Here is the code I tried: <form id ="form-signin" class="form-sign ...

Bootstrap Collapse feature fails to collapse on Twitter

I'm having trouble with the bootstrap collapse navbar. It expands fine, but I can't seem to collapse the navigation. Everything seems correct in my code: <nav class="navbar navbar-inverse nav-justified"> <div class="navbar- ...

jQuery Autocomplete - Showing array of options upon selecting input field in Internet Explorer

After encountering an issue with the autocomplete feature in a web application, I posted a question on Stack Overflow. The provided answer solved the problem in Chrome, but unfortunately, it did not work in Internet Explorer 8 or 9, and possibly earlier ve ...

Employing the findOne method repeatedly in a loop can significantly slow down operations in Node.js

Currently, I am working on a project using Node.js in conjunction with MongoDB, specifically utilizing Monk for database access. The code snippet I have is as follows: console.time("start"); collection.findOne({name: "jason"}, function(err, document) { ...

Issue with displaying value in AngularJS $scope

I am currently using flask(version 1.0.2) along with AngularJS (version 1.7.2) material (version 1.1.10). While the controller is properly attached to the view and functioning, it seems to be not displaying values in the view. This is the controller: $$ ...

Neglecting the inclusion of a property when verifying for empty properties

In the code snippet below, I have implemented a method to check for empty properties and return true if any property is empty. However, I am looking for a way to exclude certain properties from this check. Specifically, I do not want to include generalReal ...

Creating a jQuery AJAX call with a vanilla JavaScript promise

I recently transitioned my website to a Single Page Application (SPA), which involves working with only one HTML page populated using JavaScript. In order to simplify the process, I decided to consolidate my JavaScript files into one. However, while creati ...

Exploring the world of Bootstrap modals through the lens of

I have integrated AngularStrap into my application in order to utilize the modal feature of Bootstrap. However, I am facing an issue with displaying my template modal. I have followed the documentation and referenced my template with template='#test&a ...

Raycasting in Three.js originating from the mouse's position

In the process of developing an application using three.js and jQuery, I am implementing raycasting to place a helper at the point where the ray hits. My goal is to achieve a similar result as shown here. While I have successfully achieved this functional ...

Extracting data from a MySQL result array

I've encountered an issue with extracting a value from an array containing JSON data. Below is the JSON data I received (printed using console.log(rows[0])): [ { User_ID: 28, Email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email ...

Exceeding the boundaries: Bootstrap 4 Tooltip escaping the div

When hovering over an anchor tag with tooltip attributes, the tooltip appears far away from the anchor tag. Here is the LINK - Even when inspecting element it seems to be positioned too far away. <a class="btn btn-secondary" data-toggle="tooltip" dat ...

Ways to invoke a JavaScript function via a hyperlink. Various techniques to achieve this task

I want to create a div with the id of slider, and I am looking for a way to animate or hide it when a link is clicked. Specifically, I would like to toggle it open and close using a link. Javascript solution <script type="text/javascript"> ...

"Turn off sweep gesture on Surface tablet when using Chrome after pinching to zoom

In the process of developing my app, I encountered the need to disable zooming and scrolling, which I addressed using the viewport meta tag: <meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-sc ...

What is the best way to utilize JavaScript for both server-side and client-side development, all while effectively sharing code between the two

Recently, I've been brainstorming a side project that I want to dive into for fun. As I explore options, I find myself searching for examples, libraries, and frameworks that could streamline the process of sharing code between the client and server. ...

tips for performing a case-insensitive search in SQL

I have encountered an issue with this SQL query: var userLogin = "select * from login where USERNAME = ?"; The problem arises when some usernames are <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3b4f5e484f7f4e5656420a09087b5 ...

How can I make a layer visible in OpenLayers?

I can't figure out what I'm doing wrong. I believe everything is written correctly: The HTML code I have looks like this: <b>&nbspSelect Area</b> <select id="mySelect_1" onchange="showSelectedArea();" > <op ...

Tips for implementing proper validation for JSON post requests in Express.js

Looking for a better way to handle post validation in Express.JS with eloquence and DRY principles. What is the recommended approach for achieving this effectively? Below is my attempt at preventing crashes on the Node.js server and ensuring type safety i ...