AngularJS controller failing to deliver output

Just diving into the world of AngularJS, I've recently launched a new application but it seems like there's a crucial element missing when it comes to controllers.

<!doctype html>
<html lang="en>
  <head>
    <meta charset="utf-8>
    <title>Spartan Roster</title>
    <link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap.css" />
    <script src="angular.min.js"></script>
</head>
<body>

<div ng-app="spartanApp" ng-controller="SpartanController">
  <input ng-model="name">
  <span>{{age}}</span>
</div>

<script>

var spartanApp = angular.module('spartanApp', []);

spartanApp.controller('SpartanController', function($scope) {

if ($scope.name == "jon") {
$scope.age = 12;
} else {
    $scope.age = 1;
}

});

</script>

</body>
</html>

The desired functionality is as follows: When 'jon' is entered in the textbox, the output in the browser should change to 12. Any other input should result in the output remaining as 1.

Currently, it only displays 1 and does not update when 'jon' is entered. What key aspect am I overlooking regarding controllers?

Answer №1

<input ng-model="name" ng-change="onNameChange()">

$scope.onNameChange = function () {
 if ($scope.name=="jon") {
    $scope.age=12;
 } else {
    $scope.age=1;
 }
}

It is important to monitor the name change event and have your conditions handled within the change function.

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

Checking Ember.js - How to retrieve the status of each checkbox individually

I am currently working on an older version (1.11) of an Ember app and I have multiple checkboxes generated within a loop. I am looking to determine whether an element has been checked or unchecked in an action. Ember ehbs: {{#each item in data}} <lab ...

When an external image is dragged over, include the class in the drop area of the file input

Hey, does anyone know how to use Jquery to add a class when an element is being dragged over a drop area? Here's the HTML code I'm working with: <div class="upload"> <form action=""> <input class="uploadfile" type="file" ...

Use the date-fns max function to identify the latest date in an array of date strings

The Dates string array is created using data from the backend object in the following manner: const dates: string[] = this.data.map((item:any) => item.date.jsdate); Here is the resulting array: dates = [ Thu Jan 12 2015 00:00:00 GMT+0530 (India T ...

Steps to load a script when the document is ready:

What is the best method to include JavaScript using the following code: <script type="text/javascript" src="http://uads.ir/l.php?s=125125&w=5307cd5c027373e1773c9869"></script> only after the page has fully loaded? $(document).ready(funct ...

Is there a way to append more items to a store array while still being able to clear the array when resetting it?

I need to manage a player collection in my Store.js. I want to dynamically add players to the array when they are selected, and also be able to clear the array when needed. Below is an excerpt from my Store.js file: const initialState = { playerCollec ...

Issues encountered when integrating a shader

I've created a shader and I'm trying to test it on Codepen. Despite having no errors in the console, the shader still isn't working as expected. Can anyone help me figure out what's going wrong? Below is my vertex shader: <script i ...

Sorting through an array of dates in milliseconds, extracting objects with subcategories, and dynamically displaying them on the calendar based on the specific date

So I am currently working with a JSON object containing an array of objects structured like this: [ { "id": 0, "name": "Sophia Mason", "availability": [ { "date": 1522216800000, "times": ["9:00 am", "2:3 ...

Tips for incorporating conditional statements within return statements in functional components in React JS

I need to display the login page if the user is not logged in, otherwise show the forbidden 403 page. Since I'm using a functional component, I can't use render(). return forbidden === false ? ( <> <Container maxWidth="x ...

How do I prevent a specific word from being removed in a contenteditable div using JavaScript?

Attempting to create a terminal-like experience in JS, I am looking to generate the word 'current source, current location' (e.g., admin@ubuntuTLS~$: ~/Desktop) at the beginning which cannot be removed. Also, I want to prevent the caret from bein ...

Error in tabs.onUpdated argument in Firefox WebExtensions

I am currently working on developing a straightforward webExtension for Firefox and I would like to implement tabs.onUpdated with a filter. I found an example on the Mozilla website that I decided to use: const pattern1 = "https://developer.mozilla.org/*" ...

What is the best way to insert a bullet point in between items?

I am facing an issue with placing a bullet point between list items generated from ng-repeat in Angular. Despite my efforts, the bullet point keeps appearing after the items instead of between them. Here is how it currently looks: Venue1‏• $20 Ho ...

Transforming unknown JSON data into a fresh JSON structure

An undefined JSON object is being returned to a Node.js/Express.js application from a URL API endpoint http://someserver:someport/some_api_url?_var1=1. This particular JSON input always follows the same format and must be transformed into a new JSON object ...

Using JavaScript, verify if the user is connected to a network

Does anyone know of a simple method to verify if the user is able to access our website? Determine if they are not receiving a connection error. ...

ways to retrieve information from a JavaScript array in jade

I am currently working with the twitter-js-client and vis timeline libraries to create a timeline feature. In my index.js file, I parse a JSON object containing tweets using JSON.parse() and then pass this data to a Jade template to be displayed on the tim ...

Each time I scroll, the object reloads itself

I am currently facing an issue with a 3D fridge model on my website. Despite successfully loading it, I am encountering a problem where the model refreshes every time it is dragged across the screen by the user scrolling. Please refer to the linked video f ...

Steps to download a file once the submit button is clicked on an HTML webpage

I have a radio button in my application that displays file names from my Google Drive. My goal is to allow users to select a file and download it to their local device using the corresponding download URL. To achieve this, I utilize an HTTP GET request w ...

Attempting to retrieve JSON data using the subscribe method in Angular version 7.x

In my Angular 7.x application, I have a component that calls a function from a service. This function makes a GET request to a backend endpoint and retrieves an array of users. Although I can view the data within the subscribe method (where console.log is ...

Attempting to eliminate redundant data retrieved from an XML file being presented on my webpage

I need help deleting duplicate artists that appear when retrieving information from an XML file using JQuery. How can I achieve this? Check out the JS file below: $(function(){ $(window).load(function(){ $.ajax({ url: 'http://imagination ...

Having difficulty with JSON.Parse - console is showing an error in the DOCTYPE

I'm having trouble writing JSON data to a div using JQuery: var obj = JSON.parse(data); $('#container').html(obj.services.service[0].code); However, I keep receiving a Syntax error related to my DOCTYPE: <!DOCTYPE HTML PUBLIC "-//W3C// ...

Using Vue Axios to load a Laravel view can be a seamless process that

I'm attempting to create a hyperlink to a Laravel view named faq.blade.php from my Vue component. I've tried using axios, and even though it logs the response in the console, the view isn't loading. How can I resolve this issue? FaqControll ...