Incorporate Functionality into AngularJS Controller

Although I have experience with other MVC Frameworks, I am new to AngularJS and facing an issue. My controller is named "Projects" and the route is /projects. However, I want to be able to navigate to /projects/java where a new page template/view will be displayed.

I am uncertain how to achieve this in AngularJS. Is it possible to create actions for projects or should I explore alternative options?

angular
    .module('uniqueApp', [
        'ngRoute'
    ])
    .config(function ($routeProvider) {
        $routeProvider
            .when('/', {
                title        : 'Welcome',
                templateUrl  : 'views/main.html',
                controller   : 'MainCtrl',
                controllerAs : 'main'
            })
            .when('/about', {
                title        : 'About',
                templateUrl  : 'views/about.html',
                controller   : 'AboutCtrl',
                controllerAs : 'about'
            })
            .when('/projects', {
                title        : 'Projects',
                templateUrl  : 'views/projects.html',
                controller   : 'ProjectsCtrl',
                controllerAs : 'projects'
            })
            .when('/contact', {
                title        : 'Contact',
                templateUrl  : 'views/contact.html',
                controller   : 'ContactCtrl',
                controllerAs : 'contact'
            })
            .otherwise({
                redirectTo : '/'
            });
    });

Controller:

angular.module('uniqueApp')
    .controller('ProjectsCtrl', function () {

    });

Answer №1

To achieve this functionality, you can utilize a feature known as $routeParams that allows for dynamic coding of views within your application. By implementing this approach, your route configuration will appear as follows:

.config(function ($routeProvider) {
    $routeProvider
        .when('/', {
            title: 'Welcome',
            templateUrl: 'views/main.html',
            controller: 'MainCtrl',
        })
        .when('/:view', {
            title: function($routeParams){return $routeParams.view},
            templateUrl: function(params){return 'views/'+params.view+'.html'},
            controller: function($routeParams){return $routeParams.view+'Ctrl'},
        })

        .otherwise({
            redirectTo: '/'
        });
});

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

Is it Necessary to Wait for my Script Tag in React when Utilizing a CDN?

My current project involves the use of a CDN to load a script, which I am implementing through the useEffect hook directly in my component. Here is the simplified code snippet: React.useEffect(() => { const script = document.createElement('scri ...

Enhance your webpage with dynamic styling using third-party CSS animation

Apologies for any similarity to other questions on this topic. I have searched extensively, but any assistance would be greatly appreciated. I am utilizing a new animation library from Animista to animate specific elements on a test website. Animating ele ...

What is the best way to find unique characters along with their frequency in JavaScript?

Can you help me adjust this code in order to receive the desired output: "T-1 r-1 a-1 e-1 " (other characters are repeating. So no need to print the others) function checkUniqueCharacters() { var uniqueChars = []; var count = 0; var full ...

I have been tirelessly attempting to resolve this issue, yet all my efforts have proven futile thus

Encountering an issue with web packs and nextjs. import NextDocument, { Html, Head, Main, NextScript } from 'next/document' import theme from '../libs/theme.js' export default class Document extends NextDocument { render() { retu ...

Obtaining user roles from server without using JWT tokens

My main objective is to provide user roles from the backend. For instance, if a user wishes to access resources in my backend, they can log in using Google credentials. The Angular app retrieves the access token from the Google authorization server and s ...

Transforming PHP Variable Using Ajax

I have a variable called $type and I need it to be either month or year. This change should occur when a div is clicked. I attempted to use an onclick event with an ajax call. The ajax call and the variable are both in the same script (index.php). Within ...

Can you transform your content like Google does?

Looking to create a help page with a layout similar to http://support.google.com/plus/?hl=en. Can anyone provide advice or an example of how to update the new content list without refreshing the page? When you click on something like "circles and streams" ...

Using Jquery to display text when the condition is not met

Here is the code snippet I am currently working with: http://jsfiddle.net/spadez/mn77f/6/ I am trying to make it display a message when there are no fields (questions) present. I attempted to achieve this by adding the following lines of code: } else ...

Styling an active link in Next.js using Styled Components

Looking for a way to customize the active link style using styled components. I have a navigation bar where the currently active link should have a specific style applied. Any suggestions are appreciated! import React from 'react' import Link f ...

What is the process for obtaining Style.css, additional CSS, and JavaScript files from a live single page website?

I am currently facing a challenge with a live single page website where I need to make some fixes. Unfortunately, I am unable to access the Style.css file, along with other css and javascript files. Although I managed to save the html file by using Ctrl+s, ...

React - what propType should be used for the Material UI icon?

I am planning to send a Material-UI icon to the Test component and I need to define the correct proptype for it. App.js import "./styles.css"; import VisibilityIcon from "@material-ui/icons/Visibility"; import Test from "./Test&q ...

Issue with Ng-style not functioning properly when setting background color

I am struggling to set the background of an element using a configuration object with ng-style. For some unknown reason, I can't seem to make it work and I'm finding it really perplexing. The element I'm attempting to configure: <div id ...

What is the best way to differentiate between a JSON object and a Waterline model instance?

Note: I have posted an issue regarding this on the Waterline repo, but unfortunately, I have not received a simpler solution than my current workaround. Within my User model, along with default attributes such as createdDate and modifiedDate, I also have ...

What steps can be taken to customize the default keyboard shortcuts functionality in Swiper.js?

I am trying to customize the functionality for left/right keys in Swiper.js but I am unable to find a way to do this through the API () It seems that the API only allows you to disable/enable default actions: mySwiper.keyboard.enabled // Whether th ...

Creating a table in React using an object with nested objects

I have 2 different types of JSON APIs and I want to showcase them in a table. The first type has the following structure: data1:[ { "id": 1, "name": "Leanne Graham", " ...

How can we trigger a function once an ajax request has finished, without directly interacting with the ajax

I am facing a challenge where I need to trigger a JS function after an ajax call is completed, specifically when filtering posts in WordPress. The issue lies in the fact that the ajax filter tool in use is part of a WordPress plugin that cannot be modified ...

Iframe form submissions

I am interested in accomplishing the following task. My objective is to submit a form to my booking engine and show the results on either a new page or within a Div element. Ideally, when the user clicks the submit button, it would display an Iframe or re ...

What steps can I take to resolve the CSP errors I am experiencing?

I am currently working with NextJs@12 and I am attempting to set up CSP for my application. Unfortunately, I keep encountering errors in my console and I cannot figure out where I am going wrong. Below is the current policy that I have in my next.config fi ...

The collapsible sidebar on my website was functioning perfectly with Bootstrap 5, but now it seems to

I've got this sidebar that is supposed to toggle when the button in the top right corner of the photo is clicked. Currently, it's not working even though it was working fine before. I'm not sure what the issue might be. Below is the HTML c ...

The function .classList.remove() is effective when applied to one element, but fails to work on a different element

I am facing an issue where only one element is getting affected when trying to remove classes from multiple elements if certain email input requirements are met. Can someone help me understand why this is happening? Here is the code snippet: const emailI ...