Save Scope in JSON format

Currently, I am developing a web application that dynamically displays specific prompts based on the field in focus. There are several different texts stored as scripts that I want to showcase at these different points.

My goal is to incorporate scope data within these strings when they appear. Currently, everything appears as plain text. Here's what I'm currently implementing in the view (Note: I will refactor ng-init to a controller later on).

    <div><p>Full Name</p><input placeholder="Name" name="personal.name" ng-model="personal.name" ng-model-options="{ updateOn: 'default blur' }" class="medium" type="text"/></div>
    <div><p>Address</p><input class="long" type="text" ng-model="personal.address" ng-model-options="{ updateOn: 'default blur' }" placeholder="Address"/></div>
    <div><p>Phone</p><input placeholder="Phone" ng-model="personal.phone" type="phone" ng-model-options="{ updateOn: 'default blur' }" only-digits/></div>

        <div ng-init="script()">
         <div ng-repeat="scripts in script">
          <div ng-show="{{scripts.element}}" ng-hide="true" class="alert-bar">
            <div class="wrapper">
              <div ng-bind="scripts.script"></div>
            </div>
          </div>
         </div>
        </div>

Additionally, let's take a look at the controller code below (disregard any dummy data mentioned as I will handle this later):

    $scope.script = function(){

          $scope.script = [
            {
             'element': "personal.name",
             'script': '{{personal.name}} welcome to SOCU\'s loan application.'
            },
            {
             'element': "personal.address",
             'script':  '{{personal.name}}, please enter your full current address.'
            },
            {
             'element': "personal.phone",
             'script': '{{personal.name}}, please enter your current phone number, with the area code.'
            }
          ];
    }

Everything seems to be functioning smoothly except for the part where the name doesn't show up. I understand that using double curly brackets here is not the right approach. I have also attempted to fetch the data directly from the $scope object, but unfortunately, I couldn't locate any personal data there.

Answer №1

It is important to remember that controllers are meant for handling the logic of the view, not for performing bindings.

If you are iterating over a script array, you can access the elements of the object using the following notation:

{{scripts.element + scripts.script}}

When working with Handlebars syntax within a div element, make sure your array structure resembles the example below:

$scope.script = [{
    element: 'Person name',
    script: 'Your script'
}];

Here's an example of how you can use this in your HTML:

<div ng-init="script()">
    <div ng-repeat="scripts in script">
        <div ng-show="{{scripts.element}}" ng-hide="true" class="alert-bar">
            <div class="wrapper">
                <div>
                    {{ scripts.element + " , " + scripts.script }}
                </div>
            </div>
        </div>
    </div>
</div>

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

`"Type is invalid" error occurring with component after importing it into a different project``

I am currently working on developing a custom Storybook 7 Typescript component library with React. I have successfully imported this library into another project using a private NPM package. However, one of the components in the library, specifically the ...

Switching on and off a class in Next.js

I'm a beginner with Next.js and React framework. My question is regarding toggling a class. Here's my attempt in plain JS: function toggleNav() { var element = document.getElementById("nav"); element.classList.toggle("hidde ...

Generate a specified quantity of elements using jQuery and an integer

I am working with a json file that contains an items category listing various items through an array. This list of items gets updated every few hours. For example: { "items": [ { "name": "Blueberry", "img": "website.com/blueberry.png" } ...

Removing the "<!doctype html>" tag from a document using cheerio.js is a simple process that involves

Is there a way to remove and <?xml ...> from an HTML document that has been parsed by cherio.js? ?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-tran ...

Issue with controller being undefined when testing directive with controllers in Jasmine with AngularJS

I'm having trouble testing the controller that my directive is using because I can't reference the controller. directive.js (function () { 'use strict'; angular .module('myModule') .directive('c ...

Utilizing Perl to extract specific data from a JSON query

I have been working on a section of my website where I need to display information fetched from a website's Json response. The URL of the website is: . For easier code readability, you can use this JSON parser tool: . While my existing code functio ...

Easy Registration Page using HTML, CSS, and JavaScript

In the process of creating a basic login form using HTML and CSS, I'm incorporating Javascript to handle empty field validations. To view my current progress, you can find my code on jsfiddle My goal is to implement validation for empty text fields ...

Guide on adding a new member to Mailchimp through node.js and express

Hello, I've been delving into working with APIs, particularly the mail-chimp API. However, I've encountered a problem that has me stuck: const express=require("express"); const bodyparser=require("body-parser"); const request=require("request" ...

Tips for resolving issues with storing data in a text box that is constantly being added to

Can someone please assist me with this issue I'm facing? I am using isset to check if the index is defined, but it stores 0 instead of saving the value of the textbox. How should I tackle this problem? If I don't check if the index is defined and ...

Using socket.io and express for real-time communication with WebSockets

I'm currently working on implementing socket.io with express and I utilized the express generator. However, I am facing an issue where I cannot see any logs in the console. Prior to writing this, I followed the highly upvoted solution provided by G ...

Unable to access the latitude property as it is undefined

I am completely puzzled by the appearance of this issue. This snippet shows my Google Map script: <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places,geometry"></script> Below you can see that I have defined lat ...

Sending dynamic information to bootstrap's modal using props in VueJS

I'm currently working on a personal project and encountering an issue with the bootstrap modal. My project involves a list of projects, each one contained within a card element. The problem arises when I attempt to display details for each project by ...

Changing the order of a list in TypeScript according to a property called 'rank'

I am currently working on a function to rearrange a list based on their rank property. Let's consider the following example: (my object also contains other properties) var array=[ {id:1,rank:2}, {id:18,rank:1}, {id:53,rank:3}, {id:3,rank:5}, {id:19,r ...

Accessing variables from outside the query block in Node.js with SQLite

I'm looking to retrieve all the rows from a table and store them in an array called "arr". I need to access this stored array outside of the query section. Is there a way for me to get all the rows outside of the db.each function so that I can continu ...

Looking to retrieve just your Twitter follower count using JavaScript and the Twitter API V1.1?

I am trying to retrieve my Twitter follower count using JavaScript/jQuery in the script.js file and then passing that value to the index.html file for display on a local HTML web page. I will not be hosting these files online. I have spent weeks searching ...

Sending forms through the .NET platform

On my .NET page, I have implemented client-side validation for a submit button within the form element. var register = $('#<%= Register.ClientId %>'); register.click(function(){ validation.init(); return false; }); Disabling the f ...

Trouble with value updating in PostgreSQL with NodeJs

var express = require('express'); var app = express(); var pg = require('pg'); var connectionString = "postgresql://postgres:sujay123@localhost:3001/redc"; app.use(express.static('public')); app.get('/index.h ...

Using the indexOf method to arrange the data in the desired order

Greetings to all! Currently, I am working on implementing Angular fusion chart. My objective is to populate the data based on the product (json data) available. However, I am encountering an issue while utilizing the indexOf method in my fusion chart. All ...

Analyzing Two JSON Files with Different Syntax Using PowerShell

I am looking to compare a specific keyword from one JSON file to another and display it if the keyword is not present in the second file. Both JSON files have different syntax, and I want to accomplish this using Powershell: First File: Data.json [ "T ...

Is the dataType: "json" parameter necessary in AJAX requests under certain conditions?

Upon observation, it appears that there is no need to include dataType: "json" in our AJAX request when the server file is already structured in .json format. index.html $.ajax({ url: "ajax/test.json", data: "id="+id, cache: false, type: ...