Challenge with AngularJS 1.6.8: Struggling to transfer data effectively between controller and view

I seem to be lost in my code.

Here are the code snippets for all the respective files. It's a simple Single Page Application with routing.

index.html

<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Bookstore</title>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <link rel="stylesheet" href="style.css">

    <script src="angular.js"></script>
    <script src="angular-route.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/1.0.3/angular-ui-router.js"></script>
</head>
<body ng-controller="mainCtrl as vm">

    <nav class="navbar navbar-light bg-light">
        <div class="container-fluid">
            <div class="navbar-header">

                <a class="navbar-brand" ui-sref="home">Bookstore</a>
            </div>

        </div> 
    </nav> 
    <div class="container">
        <div class="row">
            <div class="col-md-2">
                <ul class="menu">
                    <li class="nav-item"><a class="nav-link active" ui-sref="dashboard">Dashboard</a></li>
                    <li class="nav-item"><a class="nav-link" ui-sref="contact">Contact Us</a></li>
                    <li class="nav-item"><a class="nav-link" ui-sref="about">About Us</a></li>
                </ul>
            </div>
            <div class="col-md-10">
               <ui-view>

               </ui-view>
            </div>
        </div>
    </div>  

    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

    <script src="app.js"></script>
    <script src="about/about.component.js"></script>
    <script src="contact/contact.component.js"></script>
    <script src="dashboard/dashboard.component.js"></script>
</body>
</html>

app.js

var app = angular.module('myApp', ['ui.router']);
app.controller('mainCtrl',function(){

});

app.config(['$stateProvider', '$urlRouterProvider',function($stateProvider,$urlRouterProvider){
    $urlRouterProvider.otherwise('/');

    $stateProvider
      .state('home', {
        url: '/',
        templateUrl: 'home.html'
      })
      .state('dashboard', {
        url: '/dashboard',
        templateUrl: 'dashboard/dashboard.html'
      })
      .state('contact', {
        url: '/contact',
        templateUrl: 'contact/contact.html'
      })
      .state('about', {
        url: '/about',
       // templateUrl: 'about/about.html',
        component: 'aboutComponent'
      })
}]);

about.html

<div ngController="aboutController">
    <div class="heading text-center">
        <h1>About Us</h1>
    </div>
    <div class="text text-center">
        <p>{{$ctrl.information}}</p>
    </div>
</div>

about.component.js

    angular.module('myApp')
    .component('aboutComponent', {
    restrict: 'E',
    scope: {},    
    templateUrl:'about/about.html',
    controller: aboutController,
    // controllerAs: 'vm',
    bindings:{
      information:'='
    }

  });

  function aboutController() {
    var vm = this;
    vm.information = 'Lorem ipsum dolor sit amet consectetur adipisicing elit. Expedita fuga quas eaque ipsa praesentium...'; // Information truncated for brevity
  };

Answer №1

To implement this feature, you have two options:

<div ng-controller="aboutController">
    <div class="heading text-center">
        <h1>About Us</h1>
    </div>
    <div class="text text-center">
        <p>{{vm.information}}</p>
    </div>
</div>

Alternatively,

You can assign the information to 'ctrl' in the component like so:

var ctrl = this;
ctrl.information = 'Lorem ipsum ...';

If you are not utilizing the component as an element and passing the 'information' in, you can remove the binding option.

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 there a way to execute a tanstack react query externally, as I am unable to utilize useQuery within the component?

const fetchSurveyData = useQuery(["survey"], () => getSurveyData({ page: 1, userLangCode: langCode, sortColumnName: "Date", sortColumnOrder: "DESC", country, ip, }) ); After tr ...

How can I modify the start of the src attribute using .attr?

This is a sample text "https://www.youtube.com/sample" to "https://www.youtube.com/sample/videos" $(".yt-user-name.spf-link").attr("href", function(i, href) { return href + '/videos'; }); I'm looking for a way to change "https://w ...

Execute code when a specific event is being attached in jQuery

When using JQuery, custom events such as .bind("foo", function(e)... are well-supported. But what if the event triggering mechanism is not yet prepared and needs to be created only on elements with the event already bound? For instance, let's say I w ...

Encountering an issue with jQuery: [ts] Expecting an identifier for $('.carousel').each(function(){})

In the process of developing an Angular project for a Multi-Item Carousel Advance which shows 1 item at a time, I have encountered a compilation error when utilizing JQuery. Despite having proper installation of JQuery, the error arises in the code snippet ...

Angular: controller's property has not been initialized

My small controller is responsible for binding a model to a UI and managing the UI popup using semantic principles (instructs semantic on when to display/hide the popup). export class MyController implements IController { popup: any | undefined onShow(con ...

Sorting tables in Vue.js with Buefy components for a user-friendly experience

In my current project, I am utilizing Vue.js. The majority of the tables I am working with use Buefy's built-in sorting feature, which I find to be the simplest solution. You can find more details in the documentation here: <template> < ...

javascript transforming a DOM layout into a hash data structure

I am looking to create a meaningful hash from specific elements within a DOM structure: <div name="stuff" class="category"> <div class="category" name="Person"> <div class="option selected" >Kennedy</div> ...

Is there a way to obtain the current URL within the index.html file using Vue.js?

How can I retrieve the current URL in my index.html file using Vue.js? When using pure JavaScript in index.html, I am only able to obtain the URL of the initial page. In order to capture the URL of other pages, I need to refresh the page as the value of ...

JavaScript can be used to create dynamic frames within a

Can anyone help identify the issue in my code? frame 1 <script type="text/javascript"> var Clicks = 0 ; function AddClick(){ Clicks = Clicks + 1; document.getElementById('CountedClicks').innerHTML = Clicks ; } localStorage.setItem(' ...

Retrieve MongoDB collection using Node.js

I am completely new to the express/mongo stack, and I have encountered an issue that I was unable to find a solution for on Stack Overflow. Here is my problem: Within my index.js file, the code looks something like this: var mongoose = require('mong ...

Troubleshooting: Brackets Live Preview feature experiencing issues with jQuery

I have been working on a note-taking website as an experiment. Everything was running smoothly in the live preview from brackets until I saved the files, and now none of the jQuery functions are working. I suspect it's an issue with my code, but it co ...

saving user information with asynchronous HTTP calls

I am encountering an issue while trying to save my form data using AJAX. When I submit the form data in a JavaScript file that calls another PHP file to perform an insertion operation, an error occurs. Here is the code snippet: <button id="submit" cl ...

Modify the selected option in a dropdown using an Angular controller

I am trying to update the "selected value" of a dropdown to match the value that is already saved in the database. Is there a way to achieve this? Controller var app = angular.module('app', []); app.controller('ctrl', function($scope ...

Tips for ensuring that your modal functions properly with an image tag within a figure tag

I'm facing an issue with getting a modal to display on my image gallery. The problem arises when the images are enclosed within figure tags, necessary for my hover effect, causing the modal to malfunction. I suspect that the problem lies within the f ...

Identifying an Incorrect Function Call in a TypeScript Function from a JavaScript File [TypeScript, Vue.js, JavaScript]

I have a vue2 application and I am looking to incorporate TypeScript into some service files without modifying the existing js/vue files. To enable TypeScript support, I utilized vue-cli which allowed me to successfully add a myService.ts file containing ...

Issue arises when there are several inline <script> tags containing parameters which result in filtered JSON output

As a newcomer to JavaScript, I am faced with the challenge of creating a centralized glossary within a confined educational environment (LMS) that lacks a database or any other facilitating elements. My objective is to develop a script hosted on our applic ...

Boost your coding speed with the Html Angular Intellisense feature in Visual Studio

I've been trying to figure out how to enable intellisense for Angular on HTML pages in Visual Studio 2013 Update 3. While I can get intellisense for JS pages using _references.js, I would like to reference controllers and properties directly in the H ...

Mastering the art of properly connecting Angular HttpPromise

Recently, I encountered an angular Service containing a crucial function: service.getItemByID = function(id) { var hp = $http({method: "GET", url: "service/open/item/id", headers: {"token": $rootScope.user.token}, para ...

Retrieving data from the firebase database by filtering based on the child's specific value

Looking at the firebase database, I can see that only the 'name' field is available. Now, I am trying to retrieve the 'quantity' value associated with that specific 'name'. Can someone provide me with a sample firebase query i ...

How can I translate a file path into byte form?

Here is the path I am working with: "file:///var/mobile/Containers/Data/Application/92C91B4A-B7F9-40F8-B8AD-646DA0607942/Library/NoCloud/Z7sY4cdv_photo_001.jpg" I am trying to figure out how to convert this file path into the actual binary representation ...