Unable to insert controller into routeprovider

Currently, I am working on an exercise in AngularJS to enhance my skills focusing on routes. However, I am facing issues with getting the controller attributes to function correctly inside the routed template. Despite reading numerous tutorials, the code snippet should be working as intended. Here is a snippet of the code:

Javascript

angular.module('greetings', ['ngRoute'])
.service("Addcontent", function($http, $q){
     this.name = "Name";
     this.familyName = "Surname";
     me = this;
     me.types= [];
     this.types = me.types;
     this.selectedOption = this.types[0];
     this.getTypes = function() {
         var deferred = $q.defer();
            $http.get("http://localhost:8090/my-site/data.json").success(function(data) {
                  me.types = data;
                 me.selectedOption = me.types[0];
                 deferred.resolve( {options: me.types, selectedOption: me.selectedOption} );
             }).error(function(){
                 console.log("error");
             });
         return deferred.promise;
     };
}).controller("renameAddContentController", function($scope, $window, $http, Addcontent, $q, $routeParams, $route){
      this.name = Addcontent.name;
      console.log(this.name);
      this.familyName = Addcontent.familyName;
      consolelog(this.familyName);
      var ctrl = this;
        Addcontent.getTypes().then(function(data) {
            ctrl.types = data.options;
        });
        ctrl.selectedOption = $routeParams.showName;
        console.log(ctrl.selectedOption);
}).config(function ($routeProvider, $locationProvider){
  $routeProvider
    .when('/greeted/:showName',
     {
        controller:'controllers.renameAddContentController',
        controllerAs: 'rename',
        templateUrl:'/my-site/greeted.html'
     })
    .otherwise({ redirectTo: '/' })
    $locationProvider.html5Mode(true);
});

The structure includes a service, a controller, and a route. The template is straightforward:

<h3>Greeting:</h3>
<span>
  {{rename.selectedOption}} {{rename.name}} {{rename.familyName}}
  <br/>
</span>

Furthermore, the main HTML content is minimal:

  <a href="/my-site/greeted/{{greeting.selectedOption}}">Greet</a>
  <span ng-view>
    </span>

(The 'greeting' belongs to another controller not included here)...

An error message indicates that the controller is not recognized as a function resulting in unresolved attributes in the template.

Any assistance would be greatly appreciated!

Answer №1

Make sure your controller name is set as:

$routeProvider
    .when('/greeted/:showName',
     {
        controller:'renameAddContentController',
        controllerAs: 'rename',
        templateUrl:'/my-site/greeted.html'
     })

The controller should be referenced as renameAddContentController, not

controllers.renameAddContentController

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 Vue v-bind:class feature does not always update immediately when the value of the binded object is changed

When utilizing Vue, it seems that developers often use v-bind:class to add dynamic classes to elements. However, in the example below, this method appears to not function as expected. //Html <div id="mainapp"> <span class="star" v-bind:class="{ ...

Tips for concealing overlay when the cursor hovers

Can anyone help me with a code issue I'm having? I want to hide an overlay after mouse hover, but currently it remains active until I remove the mouse from the image. Here is the code: .upper {position: absolute; top: 50%; bottom: 0; left: 50%; tra ...

Tips for identifying when a date has been chosen in Angular Bootstrap datepicker

Currently, I have implemented the datepicker directive from Angular Bootstrap in the following manner: <datepicker ng-model="period_start" show-weeks="false" class="bs-dateselector"></datepicker> I am looking for a way to identify when a user ...

Error in AWS Cloud Development Kit: Cannot access properties of undefined while trying to read 'Parameters'

I am currently utilizing aws cdk 2.132.1 to implement a basic Lambda application. Within my project, there is one stack named AllStack.ts which acts as the parent stack for all other stacks (DynamoDB, SNS, SQS, StepFunction, etc.), here is an overview: im ...

Tips for displaying a restricted quantity of items in a list according to the number of li lines used

My approach involves using a bulleted list to display a limited number of items without a scrollbar in 'UL' upon page load. On clicking a 'more' button, I aim to reveal the remaining items with a scrollbar in UL. The following code acco ...

Sending a Variable and List to a Controller using Ajax

I am having an issue with passing data from a Text Box and a Select Options Multiple using knockout selectedOptions in a viewModel to my Controller via ajax. I am unable to receive the MetricsChosenModel information. var MetricsChosenModel= wi ...

Is there an efficient method for transferring .env data to HTML without using templating when working with nodejs and expressjs?

How can I securely make an AJAX request in my html page to Node to retrieve process.env without using templating, considering the need for passwords and keys in the future? client-side // source.html $.get( "/env", function( data ) {console.log(data) ...

Extract data from JSON in Google Sheets

UPDATE: entry.content.$t is actually not the correct field to access individual cells. The proper method is using entry.gsx$[cell column header]. Thank you for pointing out this mistake and assisting in finding a solution. Initial inquiry: I am currently ...

Execute a function with parameters when a button is clicked dynamically using PHP

I am trying to execute a parameterised function in PHP using AJAX. Below is the code snippet of my dynamic button where I need to pass $sub_id to the delet(sub_id) function for executing some SQL: echo "<td><input type='submit' name=&a ...

Implement a fadeout effect by using a timeout function that adds a class to the div element in

I implemented a loader on my webpage that animates for 3 seconds before a function kicks in to modify the styles and make it invisible. I am looking to add a fadeout effect when the 'waa' style is applied to the 'load' div. setTimeou ...

Is there a method available to minimize the size of a local storage list containing strings?

Hey there, I am trying to load a large 2.5MB json file in my browser so that I can use it for some typeAhead functions. Unfortunately, I'm facing an issue with my local storage being constantly full. When using Firefox, I receive the following error ...

Ajax-powered Datatables

I am a beginner to data tables and I am attempting to retrieve data from a JSON text file (test1.txt). Below is an excerpt of the data present in the file, which contains over 5000 entries: [{"0":"22352442","ID":"22352442","1":"22126303","PARENT":"2212630 ...

Step-by-step guide to enabling native Bootstrap 2.3.2 JavaScript elements within Liferay

In my web development project, I am working with Liferay 6.2 and utilizing Alloy UI's version of Bootstrap in the html pages by adding these lines: <link href="http://cdn.alloyui.com/2.5.0/aui-css/css/bootstrap.min.css" rel="stylesheet" /> < ...

What is the right way to send a success response from Express JS to Backbone when logging out a user?

I'm currently learning how to work with Express JS and Backbone. On the server side using Express.js, I have this code snippet for logging out a user: app.get('/logout', function(req, res) { req.logout(); res.send('How can I ...

Bring the element to the top of the page by clicking on the anchor within the element or anywhere within the specified div ID

I am looking to implement a functionality where the page scrolls to the top of the navigation div ID when a link inside the navigation div is clicked, or ideally even when clicking anywhere within the div itself that contains the navigation links. After r ...

Redirecting in Next.js from an API route

I am in the process of developing a backend application that necessitates user authentication. Within this project, I'm utilizing 2 external APIs: API A: responsible for managing user accounts and sessions API B: utilized for executing CRUD operation ...

Personalize the jquery autocomplete outcome

I'm currently utilizing jQuery autocomplete along with a remote data source. $( "input#searchbar" ).autocomplete({ source: function( request, response ) { $.ajax({type: "post", mode: "abort", dataType: ...

A guide on arranging map entries based on their values

The map displayed below, as represented in the code section, needs to be sorted in ascending order based on its values. I would like to achieve an end result where the map is sorted as depicted in the last section. If you have any suggestions or methods o ...

Iterate through each key in the response JSON object using a variable named "a

Here is a snippet of my code: var roomid= roomIds[i] const Availabilitydata = await AvailResponse.json(); availableroomsArray.push(Availabilitydata); app.get("/api/availability", (req, res) => { res.json({ indicateur: availableroomsA ...

Express.js and Node version 0.10.29: The Mystery Post

Having trouble with sending JSON data to an express server and receiving 'undefined' for req.body.name. This is how the configuration is set up: const express = require('express'); const app = express(); app.configure(function(){ ...