Issue with displaying Angular variable on HTML page

I'm trying to brush up on my Angular skills, but I'm having trouble grasping certain concepts. The "controller as" pattern is giving me trouble - it's supposed to be simpler than using $scope, but it's not working for me.

Specifically, I can't seem to get a basic variable to display in my HTML.

Let's take a look at the code in question:

app.js

angular
    .module('routerApp', [''])
    .controller('mainController', function () {
        'use strict';
        var vm = this;
        vm.bigMessage = 'A smooth sea never made a skilled sailor';
    })

index.html (simplified version)

<!DOCTYPE html>
<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js"></script>
        <script src="js/app.js"></script>
    </head>
    <body class="container" ng-app="routerApp" ng-controller="mainController as main">
        <h1>{{ main.bigMesssage }}</h1>
    </body>
</html>

Interestingly enough, when I check the browser source code, it doesn't render "{{ main.bigMessage }}" - it just shows nothing. But the {{ ... }} part is definitely in the code.

Answer №1

The issue here is that you mistakenly added an empty string in the dependency array bracket in Angular, which causes Angular to look for a dependency with no name. This results in an error being thrown by Angular's $injector. Since you don't have any dependencies, the correct syntax should be [].

Corrected Code:

angular
.module('routerApp', [])
.controller('mainController', function () {
    'use strict';
    var vm = this;
    vm.bigMessage = 'A smooth sea never made a skilled sailor';
})

Additionally, there is a typo in {{main.bigMesssage}}, where bigMesssage should actually be bigMessage. The correct syntax should be:

{{main.bigMessage}}

View Demo Plunkr

Answer №2

you have accurately written everything except for a typo in your bigmessage variable spelling

 <body class="container" ng-app="routerApp" ng-controller="mainController as main">
        <h1>{{ main.bigMessage }}</h1>
    </body>

controller

angular
    .module('routerApp', [])
    .controller('mainController', function () {
        var vm = this;
        this.bigMessage = 'A smooth sea never made a skilled sailor';
    })

take a look at this fiddle: https://jsfiddle.net/bjwov6f1/1/

The issue was not finding your variable, which is why it was not displaying anything

for further learning, visit this link:

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 focus() function fails to execute properly in Vue for NativeScript when v-for is used

One of the challenges I'm facing involves setting focus on the first textfield when a button is pressed. I found a code snippet in the playground without using v-for and it works perfectly fine. However, as soon as I introduce v-for into the code, e ...

Transmit information from JavaScript to a servlet and receive a response in return

I'm new to using ajax and JavaScript. Currently, I have a simple jsp page containing HTML and an accompanying JavaScript file with all my functions defined. One of these functions sends JSON data to a servlet and now I need assistance in receiving th ...

Get items from an array that have a property matching another array

Is there a more efficient way to create a new array with contact objects that have values matching those in selectedContact? selectedContact: number[] = [0,2] //value contacts: Contact[] = [{ firstName:"Dan"; lastName:"Chong"; email:"<a href="/c ...

convert data from IndexedDB to a JavaScript array

Hello, I am quite new to web development and currently experimenting with creating an "offline" application using vue.js and dexie.js. Dexie.js provides easier access to Indexed Database on modern browsers. However, I believe my issue lies more in the bas ...

Encountering issues with resolving dependency tree post updating node, specifically with node-sass dependency causing failure

Following the update to the latest version of Node.js, I encountered error messages when attempting to use npm audit fix --force. It appears that resolving dependency tree issues is proving to be difficult. Despite extensive research and trying various s ...

Using NodeJS to retrieve the mean price from the OPSkins pricelist

Is there a way to calculate the average price of every skin listed in this document? I'm wondering how one would go about using the dates and prices in order to determine the 60-day average price. My approach would involve extracting the data from t ...

What sets ngInclude apart from ngRoute?

I was contemplating the idea of switching to ngRoute because it seems to be getting a lot of attention lately. However, I've found that ngInclude works perfectly well for my needs and I can't quite understand why everyone is gravitating towards n ...

Can you display a popup on a leaflet map from beyond its boundaries?

Utilizing leaflet maps, I have implemented a functionality where clicking on the map opens a popup at a specified location and centers on that position. The code for this is as follows: var popup = L.popup(); function onMapClick(e) { popup . ...

Retrieving information from a JSON response using Node.js

Using an API, I am retrieving product details in the form of a JSON object. { "productBaseInfo": { "productIdentifier": { "productId": "EKTDDD23232zYHR94E4", }, "productAttributes": { "title": "Nova KT 7 ...

How can callbacks be utilized with useState to mimic the behavior of setState in class components?

In my application, there is a dynamic table with rows of text fields that users can edit and add new rows to. Whenever a user clicks the 'Add' button to create a new row, I want the focus to be set on the first text field of the newly added row. ...

What is the way to access the Ember global variable 'App' within an Ember CLI application?

As I develop my Ember application with the Ember CLI, I encountered an issue while trying to access the global App variable in order to create and insert a component into my layout. The error message I received was "Uncaught ReferenceError: App is not defi ...

How can I implement an alert to pop up when a user selects an option in JavaScript?

I've tried multiple solutions and searched extensively, but I can't seem to get JavaScript to alert my selected option. Why isn't it displaying an alert after selecting an option? const selects = document.querySelectorAll('.group-sel ...

Creating dynamic flags with specific parameters using Pentaho and JavaScript

I am looking to optimize my code by finding a better way to achieve this. I have a variable called "hour" and I need to create flags for each hour of the day like so: if (hour == 0) {flag12AM = 'yes'} else {flag12AM == 'no'} if (hour ...

wiping out a column in Excel spreadsheets with the help of Node.js or its corresponding node modules

I've attempted various approaches without success. Can you provide guidance on deleting and adding columns within an Excel sheet using Node.js? ...

Incorporate a background image with the JavaScript CSS property

I'm having trouble adding a background image using the Javascript CSS property in my code. When I directly add the 'url', it works fine. Could the issue be with the 'weatherImage' variable? Javascript var OpenWeatherKey = ' ...

Challenges with handling Ajax responses in Ruby on Rails

I'm currently facing an issue with the Ajax response in my Rails application, and I'm unsure of how to troubleshoot it. Here is the code that is functioning correctly: View <div id="<%= dom_id(comment) %>_count"> <%= Like.wh ...

Tips for updating page content without needing to refresh the page

Have you noticed on Foursquare's website: They have new feeds appearing automatically without the need to refresh the page. I'm working on a backend system to display user specific feeds. How can I achieve the same effect as Foursquare? ...

Set a looser Cross-Origin Resource Policy to avoid blocking a particular resource

I am in the process of building a fullstack app using PHP, React JS, and graphQL. I attempted to deploy the app to 000webhost but encountered an error. Upon checking the network, I noticed that I received a 200 status response, however, the data is being r ...

Guide to incorporating navigation buttons (back and forward) in a modal box

I have successfully created image thumbnails and linked them using the provided code. <!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery-2.2.3.min.js"></script> <script src="https://maxcdn.bootstra ...

When the awaiting fetch operation completes, it yields two separate arrays of data that are inaccessible for immediate use

I've encountered a problem while working on my full-stack project that uses Express in the back-end and React in the front-end. The issue lies with a specific component designed to fetch results from a database query located at /api/blogposts. Below ...