Utilizing this method for declaring functions in Angular

I'm currently learning about Angular and attempting to implement a controller. However, I've run into an issue where the ng-click is not working (the function is not being called). According to the Chrome extension Batarang, the function is defined as null. Can anyone explain why?

angular.module('starter', ['ionic'])

.run(function ($ionicPlatform) {
    $ionicPlatform.ready(function () {
        if (window.cordova && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        }
        if (window.StatusBar) {
            StatusBar.styleDefault();
        }
    });
})


.controller("TodoControl", function Controller() {
    var thisRef = this;

    this.todos = [
        {
            text: "my todo 1",
            done: false
        },
        {
            text: "my todo 2",
            done: true
        }
    ];

    this.newEntry = "hello";

    this.AddEntry = function () {
        console.log("adding");
        thisRef.todos.push({
            text: this.newEntry,
            done: false
        });
        thisRef.newEntry = "";
    }

});
<body ng-app="starter">

    <ion-pane>
        <ion-header-bar class="bar-stable">
            <h1 class="title">Ionic Blank Starter</h1>
        </ion-header-bar>
        <ion-content>
            <div ng-controller="TodoControl as ctr" class="container">
                <form role="form">
                    <div class="form-group" ng-repeat="t in ctr.todos">
                        <div class="checkbox">
                            <label class="checkbox-inline">
                                <input class="form-control todoFormControl" type="checkbox" ng-model="t.done"><span>{{t.text}}</span></label>


                        </div>
                    </div>
                </form>
                
                
                <label><input class="todoNewTaskIn" type="text" placeholder="New Task" value="{{ctr.newEntry}}" Style="border: 1px solid gray;" ><button class="btn" ng-click="ctr.AddEntry()">Add</button></label>
                
            </div>
        </ion-content>
    </ion-pane>
</body>

This application uses Ionic (which includes Angular).

Edit: Here's a JSFiddle link for reference: http://jsfiddle.net/duowfLsn/

Answer №1

To make it work, simply move your button out of the section. Check out the updated example below:

<label>
<input class="todoNewTaskIn" type="text" placeholder="New Task" value="{{ctr.newEntry}}" Style="border: 1px solid gray;" >
<button class="button" ng-click="ctr.AddEntry()">Add</button>
</label>

This code should be changed to:

<label>
<input class="todoNewTaskIn" type="text" placeholder="New Task" value="{{ctr.newEntry}}" Style="border: 1px solid gray;" />
</label>
<button class="button" ng-click="ctr.addEntry()">Add</button>

You can view the revised version at http://jsfiddle.net/duowfLsn/3/

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

Using Vue and Vuex to wait for asynchronous dispatch in the created hook

I'm struggling to implement asynchronous functionality for a function that retrieves data from Firebase: Upon component creation, I currently have the following: created(){ this.$store.dispatch("fetchSections"); } The Vuex action looks ...

Searching in AngularJS yields unexpected results beyond what was anticipated

I am attempting to retrieve all the users from my Rails application This code is in my Javascript file: var app = angular.module('app'); app.factory('User', function($resource) { return $resource('/users/:id', {id: &apos ...

Using jQuery to bind the "CTRL+A" key combination to exclusively selecting a specific region

My goal is to modify the CTRL+A shortcut so that instead of selecting all text, it will only select specific content within containers with a class of xyz. Unfortunately, I have not been successful in getting this functionality to work. Even attempting to ...

Tips on concealing the divs following the second click and a page refresh

Bootstrap 4 js collapse is working fine, but I am facing an issue. When I click to show a hidden div and then reload the page, clicking again reveals the previously hidden div when it should be hidden. Any suggestions for addressing this problem? ...

In the world of three js, we are presented with two distinct scenes featuring CubeGeometry and PointsGeometry. However

I've been trying to generate a cube and some dots that are part of a torus using the code below. However, I can only see the cube and not the dots. Despite spending hours searching for the dots, they remain elusive. // Generating a cube cu ...

Revamping status and backend systems

Seeking advice on the most effective method to execute a HTTP PUT request within my react application. A Post component is responsible for fetching data from https://jsonplaceholder.typicode.com/posts/1 and displaying it. There's another component na ...

Tips for choosing multiple select options using selenium

Currently, I am in the process of developing a test using Selenium Webdriver. My goal is to automatically select the second option from every dropdown menu that may appear on the page. It's worth noting that the number of dropdown menus will vary with ...

Creating a bower.json file similar to requirements.txt

After diving into the world of bower, I've found it to be incredibly useful. Coming from a python background, I'm accustomed to working with virtualenv and requirements.txt. Given that I prefer not to keep all my dependencies in source control, ...

Trapped in the JavaScript Checkbox Filter Maze

After successfully creating a javascript-only filter, I have hit a roadblock and could really use some assistance. The filter is divided into "days" and "events". When a user clicks on a day or multiple days, the events for those selected days are displa ...

"Exploring the process of comparing dates using HTML, AngularJS, and Ionic

I am working on an HTML file that shows a list of notification messages. I am trying to figure out how to display the time difference between each notification. The code snippet below displays the notifications and includes the time for each one: <ion- ...

Challenges encountered when executing a node function in an AWS Lambda function

I have observed some unusual behavior with my AWS Lambda function. Here is the code snippet for the Lambda: import { TwitterApi } from 'twitter-api-v2'; const client = new TwitterApi({ appKey: 'APP_KEY', appSecret: 'APP_ ...

Dealing with Content-Range for pagination in Restangular: A comprehensive guide

When working with an API that returns the header "Content-range" for lists, how can I efficiently retrieve and parse this header value using Restangular? Do I need to create a custom function for this task? Is there an existing method in Restangular that ...

The correct method for ensuring a promise is successfully handled (with the inclusion of the finally clause) before making an assertion in Vue.js and Jest

I am facing a situation where I have a component with behavior similar to the one below: @Component export default class MyComponent extends Vue { public result: number = 0; public ready: boolean = false; // for test purpose only. In the real cod ...

Issues with IonPicker displaying incorrect values

In my Ionic App, I am using the ion-picker component to display the selected text from an array of options. Although everything seems to be working fine and the console.log accurately displays the index value, there is a strange behavior when it comes to s ...

Navigation issue discovered while trying to implement Ionic's collection-repeat feature

As a newcomer to Ionic and Angular.js, I recently downloaded an example of an ionic collection repeat and navigation from http://codepen.io/ionic/pen/mypxez. Initially, the example worked perfectly with just single index.html and index.js files. However, I ...

Accessing a model's field within an Ember.js each loop

Here is the code for a route that I am working on: Calendar.DateIndexRoute = Ember.Route.extend({ model: function(data) { return {arr:getCalendar(data), activeYear: data.year, activeMonthNumber: data.month, activeDay: data.da ...

After activating the rewrite feature on Tomcat valve, JavaScript is loading twice

I've implemented the Tomcat rewrite valve in my single-page application to direct all requests, except for static resources, to my index.html file. Here is what my rewrite.config looks like: RewriteCond %{REQUEST_URI} (?!.*\.(?:jpg|png|css|js|js ...

Tips for ensuring the .selected class remains on the correct a(href) after a page reload in a list of hrefs

Every time I click on a link in my sidebar, the list of links reloads. However, when I move to the next page, the "selected" class goes back to "all" instead of staying on the chosen link. I've looked at explanations for tabs, but I couldn't get ...

The show.bs.modal event has been triggered when the .show() method is used on elements within the

Recently, I discovered that the event show.bs.modal is triggered not only when the modal itself is shown, but also every time you call the .show() method for an element within the modal. To attach the event handler, you would typically use the following c ...

Difficulties with validating phone numbers

I'm having an issue with my JavaScript code that is supposed to validate a phone number field, but it doesn't seem to be working. Even if I enter incorrect values, the form still submits. Here's the snippet of my code: <script> functi ...