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

Incorporate a new substance into a THREEJS Mesh

Is there a way to add a material to an existing mesh without calling the mesh constructor again? I already have a mesh created using the constructor: var mesh = new THREE.Mesh(geometry, material); Now I have created another material in my code: ...

Comparing $.fn.fancybox and $.fancybox: What sets them apart?

I'd like to understand the distinction between the two items shown above. In what ways do they differ from each other? ...

Tips for moving a div with a button in jQuery

I am working on a parent div that has an overflow set to hidden and it contains dynamically created children. I want to be able to scroll the parent div using two buttons - one for scrolling up and the other for scrolling down, as if the div had overflow-y ...

Oops! Looks like something went wrong. The command to install the debug app failed because the emulator could not be launched. This could be due to the fact that no emulators were found

I'm in need of assistance to resolve these issues Encountered an error while trying to install the app. Please ensure that your Android development environment is properly configured: https://reactnative.dev/docs/environment-setup. Error: Command fai ...

Guide on crafting a scrollable and touch-responsive grid using CSS and JavaScript

I am seeking guidance on creating a grid with a variable number of columns and rows. It should be contained within a separate div and should not interfere with other objects or alter the parent's size. The grid needs to be square and I am currently u ...

`` I am experiencing a problem with CSS display block in Internet Explorer

Looking to implement a basic tab menu using a combination of tables and div elements. The goal is to have the tabs switch styles when clicked, giving the appearance of being toggled, while also swapping out the corresponding content in separate div blocks ...

Angular JS unit test for if else statements

I am looking to enhance the code coverage of the branch section by conducting unit tests on the if else statement. However, I am unsure about what exactly needs to be tested in the following code snippet. Controller.js (function () { "use strict"; ...

Having trouble getting CSS3 Keyframes to function properly?

Check out the following code snippet: .startanimation { height: 100px; width: 100px; background: yellow; -webkit-animation: animate 1s infinite; } @-webkit-keyframes animate { 100% { width: 300px; height: 300px; } ...

Dynamic form validation using jQuery

I am facing a challenge with validating a dynamic form on the user side. My goal is to ensure that if a user fills out one column in a row, they are required to fill out the remaining columns as well. For example, filling out the CC # should prompt the use ...

showing the data in a textbox using AJAX retrieved information

I have a table where additional rows can be added by the user with the click of a JavaScript function. Each row contains a drop down list, and based on the selected value, an AJAX script fetches some values to display in corresponding textfields within th ...

How can I implement a scroll functionality to navigate to the next item in a Vuetify v-carousel?

I'm currently working on a front page layout using the v-carousel component and I am looking to achieve automatic scrolling to the next item without the need for arrows or delimiters. How can I make this happen? Below is my current code: <template ...

Is using float:right making the jquery slide toggle dropdown div (triggered by hover) appear glitchy?

Utilizing jQuery's slidetoggle and hover functions, I have successfully implemented a dropdown feature in my div. Within this div, there is various information displayed including the date, a note counter, and three buttons. The first two are Tumblr ...

Increasing the size of text in CSS with the use of ":hover" and then causing it to return to its original smaller size

Allow me to explain my goal here. I have text displayed on my website that I would like to enlarge when the user hovers over it and then shrink back down when they move their cursor away. The issue I'm facing is that after enlarging the text using t ...

Tips for creating a div that slides from the right side to the left side

I received a code from someone and I need help with making the sliding div slide from right to left. Essentially, I want the div to hide on the right side and slowly move to the left within a width of 300px. Here is the HTML code: <a id="loginPanel"&g ...

JavaScript object conversion to Java Model is proving to be a challenge

When looking at my Spring controller and client-side Javascript code, I noticed that the Javascript object is having trouble reaching the Spring controller in Object form. Here is a snippet of my Controller code: @RequestMapping(value = "/addRating", meth ...

Implementing a versatile free text filter with numerous values using MUI

I am currently working on incorporating a free text filter view using MUI. Although the Autocomplete component already has this feature (when the 'multiple' attribute is enabled), I am looking to input free-form text instead of selecting from pre ...

How to disable a specific array of dates in Ant Design range picker

Is there a way to block dates prior to the current date and specify certain dates that should also be disabled on the calendar? For example, I need to prevent selection of: Today: 2018-07-30 Dates to disable: [2018-08-10, 2018-08-12, 2018-08-20] When t ...

What is the best way to conceal a ModalPopupExtender that is integrated into an ASCX user control without triggering a postback, utilizing JavaScript?

I am currently working on an Asp.net application and have encountered an issue with the ModalPopupExtender embedded within an ASCX user control. I am trying to set up a cancel button that will hide the popup without triggering a post back when clicked. He ...

Conversation with form component in angular2

I am currently using ng2-bootstrap-modal. For adding a sample form to the example Confirm Dialog, check out ng2-bootstrap-modal. To change the template: <div class="modal-dialog"> <div class="modal-content"> <form [formGroup]="login ...

"Implementing a dynamic search for IMG SRC using ng-src in AngularJS: a step-by-step

I am encountering an issue with setting the source of an image based on a variable that may initially be null. When I set the source to include the path and the variable followed by PNG, I get an error because the variable is null at the beginning. How can ...