Illustrative demonstration of AngularJS

One way to showcase data using AngularJS is by triggering a function with a button click. Here's an example:

<!DOCTYPE html>
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>

<script>
function Display($scope){
    $scope.show_date = function(){
        $scope.show=new Date();
    }       
}   
 </script>
</head>
<body  ng-controller="Display">
<button ng-click="show_date()">Show date!</button>
<div ng-message="required"> {{show}} </div>
</body>
</html>

What mistakes can you find in this code snippet?

Answer №1

If you are using an angular version above 1.3, please note that global controller function declarations are no longer supported starting from this version.

To resolve this issue, update your controller as shown below:

var myApp = angular.module("myModule", []);
 myApp.controller("Display", function ($scope) 
 {
    $scope.show_date = function(){
    $scope.show=new Date();
  }     
 );

Check out the DEMO here!

Answer №2

Just a couple of things to do:

Start by defining an instance of your app module like this:

angular.module('app', [])

Next, register your controller with this app module:

.controller('DisplayCtrl', DisplayCtrl);

Don't forget to click on Run code snippet to see your code in action.

function DisplayCtrl($scope){
    $scope.show_date = function(){
        $scope.show=new Date();
    }       
}   

angular.module('app', [])
    .controller('DisplayCtrl', DisplayCtrl);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app" ng-controller="DisplayCtrl">
  <button ng-click="show_date()">Show date!</button>
  <div ng-message="required"> {{show}} </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

Substitute the functions within a Node.js module with simulated functions

I am currently working on a Node.js project that serves as an API wrapper. To ensure the reliability of my code, I am writing unit tests using nodeunit and need to incorporate mock functions into the module. For instance, I require a function that mimics s ...

Jersey receives null object when Angular JS posts data

I am currently facing an issue where the form data I am trying to post to my rest service using angular js and jersey is not being received properly. The bean that should be populated with the data at the rest service end is always null. Below is a snippet ...

When working with Node.js and Express, encountering the error message "data.push is not a

I am encountering an issue when trying to add a new task to the list of all tasks. The error message I receive is TypeError: allTasks.push is not a function data contains JSON data that has been retrieved from a file using the fs method var allTasks = JS ...

Securing an AngularJS page with Spring Security is not achievable

I've implemented Spring Security to secure my application using the configuration below in an attempt to display the default Spring login page: spring-security.xml <beans:beans xmlns="http://www.springframework.org/schema/security" xmlns: ...

Using jQuery and JavaScript: The recursive setTimeout function I created accelerates when the tab is no longer active

I am facing a unique challenge with my jQuery slideshow plugin that I'm currently developing. Although the code is running smoothly, I have observed an issue where if I leave the site open in a tab and browse elsewhere, upon returning to the site (us ...

Automatically update button appearance upon reaching zero value using JavaScript

Whenever I click the button, the user's HP decreases until it reaches 0 and then the button changes. However, a peculiar issue arises when the userHealth hits zero - the button does not change immediately. An additional click is required for the butto ...

Using Ternary Operators in React Components for Styling

Extracting data from a json file and attempting to assign a specific class only when certain criteria are met: render: function() { var gameList = this.props.data.map(function(game) { return ( <li key={game.id} className="l ...

Instructions for adding an onfocus event listener to an input field in order to dynamically change the formatting of associated labels

I'm looking to change the style of my input labels to a more fancy look by implementing the .fancyclass style when using the onfocus event on the input field. I am curious to know how this can be achieved through event listeners in Javascript? ...

Troubleshooting issues with calculator operators in JavaScript for beginners

As a beginner in JavaScript, I've taken on the challenge of building a calculator to enhance my skills. However, I'm having trouble getting the operator buttons (specifically the plus and equal buttons) to function properly. Can someone please as ...

jQuery sidebar with a fixed position

Is there a way to implement a sidebar menu feature using jQuery that reappears on the left as the user scrolls down the page? You can see an example sidebar menu here. ...

What could be causing the 401 status code in my sign_in.json request? (Guide on AngularJS and Rails from Thinkster)

Currently, I have been diligently following the AngularJS Tutorial for Rails on Thinkster and am now approaching the conclusion of User Authentication with Devise. The application appears to be functioning properly on my local server, but upon inspecting t ...

Enable users to handle the version of a dependency in an npm package

As I develop a module that relies on THREE.js, I am exploring the most effective method to include THREE as a dependency and ensure accessibility for both the module and its users. My goal is to provide users with access to the THREE library within their p ...

Tips for handling daily JavaScript tasks using Angular JS

Over the past few days, I've been diving into AngularJS. While it seemed intuitive in tutorials and videos, when I actually started replacing my current web app code with AngularJS, I encountered numerous issues. For instance, if I wanted to add HTML ...

The div is obscured by the background image

Could someone please assist me in preventing the .background image from overlapping with the skills div when the viewport expands either vertically or horizontally? I've tried several approaches without success. Any help would be greatly appreciated! ...

Can you identify the target of the term "this" in the upcoming JavaScript code?

DISCLAIMER: I am inquiring about a specific instance of this, not its general purpose. Please refrain from quick Google responses or copied answers (: The code snippet below demonstrates JavaScript/jQuery: var req = {}; function getData() { var from ...

What is the best way to eliminate mouse click release events?

Encountering an issue with my Vue/Vuetify Dialog that closes when clicking outside of it as expected. The problem arises when there is a text field inside the dialog. If I accidentally select the content and release the mouse outside of the dialog, it als ...

What are the steps for modifying the JSON data format in AngularJS?

As a newcomer to Angular JS, I am working with the following JSON data: { "CheckList": [ { "UnitClass": "Budget Space", "CheckListCategoryId": 1, "CheckListCategory": "DOORS", "CheckListItemId": 2, "CheckListItem": "Deadbolt, Lockse ...

What is the best way to retrieve a Promise from a store.dispatch within Redux-saga in order to wait for it to resolve before rendering in SSR?

I have been experimenting with React SSR using Redux and Redux-saga. While I have managed to get the Client Rendering to work, the server store does not seem to receive the data or wait for the data before rendering the HTML. server.js ...

`Check out Vue3's property watching feature`

Currently, I have a form that is being edited and the created method is used to prefill the form information from an api call, which works perfectly fine. However, my goal is to monitor the fields in the form. If any of them are edited, I want to set a va ...

What is the best way to store chat messages in a React application?

My idea for a chat application using React involves saving chat messages in localStorage. Below is the code snippet that illustrates this functionality: const [textMessages, setTextMessages] = useState([]); const [textValue, setTextValue] = useState(' ...