Utilizing AngularJS to bind to prototype methods

I recently started delving into angularjs and encountered an issue which I suspect might be due to my improper use of it. I have a view connected to an array of objects; I aim to define properties and methods for these objects in a viewModel. This involves using a constructor function for the property and adding methods to the prototype. Below is the JavaScript viewModel code:

var CDLViewModel = function(name, operator, state) {
    this.Name = name;
    this.Operator = operator;
    this.State = state;
    this.LockEnabled = false;  // some logic based on the state here
};

CDLViewModel.prototype = {    
    StartEnabled: function() {
        return false; // some logic based on the state here
    }  
};

Within this viewModel, there are two methods controlling the enabling of buttons - one defined in the constructor and the other in the prototype. My objective is to solely define all methods within the prototype, but it seems that Angular does not recognize the methods defined here. During the execution in controller, data is fetched from the server following which the array of viewModels is populated. Upon debugging the array, I can observe the objects with the method StartEnabled in the prototype:

function myController($scope, $http, $rootScope) {
    if (!$rootScope.cdls) {
        $rootScope.cdls = [];
        $http.get("http://localhost:8080/...")
            .then(function(result) {
                for (var i = 0; i < result.data.length; i++) {
                    $rootScope.cdls[i] = new CDLViewModel(result.data[i].Name, result.data[i].Operator, result.data[i].State);
                }
            },
                function() {
                    alert("ko");
                });
    }
};

The view consists of markup that iterates through the array and binds certain input elements along with button states to each viewModel. Strangely, the button linked to the LockEnabled method functions properly while the button associated with the StartEnabled method remains disabled consistently. Below is an extract from the view:

<tabset>
    <tab data-ng-repeat="cdl in cdls" heading="{{cdl.Name}}" >

     Operator: <input type="text" data-ng-model="cdl.Operator"/>

        <a class="btn btn-primary btn-lg btn-block" href="#/start" data-ng-disabled="cdl.StartEnabled" >Start</a>

        <a class="btn btn-primary btn-lg btn-block" href="#/lock" data-ng-disabled="cdl.LockEnabled">Lock</a>

The second button/link works as intended, however, the first one does not.

Answer №1

In my opinion, the

data-ng-disabled="cdl.StartEnabled"
needs to be updated to
data-ng-disabled="cdl.StartEnabled()"
in both instances.

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

Can you use a lightbox to showcase multiple images while only displaying one image at a time to trigger them?

Currently, I am utilizing Lightbox in order to showcase photos. For example, I have divided the photos into two categories: "work" and "vacation". To implement this, I would follow this code snippet... <a href="images/image-1.jpg" rel="lightbox[work]"& ...

Inject the JSON data fetched through AJAX into Datatables

I have been successfully using the datatables plugin to populate multiple tables with data. However, I realized that instead of making separate AJAX calls for each table, I could optimize by fetching the data once and storing it in a variable to be used by ...

How can I invoke a jQuery widget's public method from the outside using the plugin reference?

I have encountered an issue with a plug-in popup that I created. I am attempting to reference and access the public method of the plug-in, but I am receiving an 'Undefined Function' error. var pop = $('.divCls').popup(); pop.setTit ...

Here is a step-by-step guide on creating a custom select all checkbox in the toolbar of a MUI

I am currently using the MUI data grid to build my table, with the following properties: <DataGrid rows={serialsList || []} columns={columns} rowsPerPageOptions={[25, 50, 100]} //pageSize={93} ...

With vuejs, only one place can control the changing of v-model

Hello, I am currently working on integrating VueJS2 code with Laravel Blade. However, I have encountered an issue with the following VueJS2 code: new Vue({ el:'.add_item_to_price_menu', data:{ percentage:null, }, methods: ...

Sorting Questions by Category

I am looking to organize a set of quiz questions into three different categories - Math, English, and Science. Is there a way to label each question within the array with its respective category and create a function that calculates scores based on these c ...

How can I generate a clickable link that will open in an iframe when clicked?

In this scenario, I am attempting to generate a link by using different IP addresses. P1 represents one IP address, while P2 indicates the host. Upon clicking, the intention is to create a link and open it within an iframe. <body> p1: &l ...

Husky and lint-staged failing to run on Windows due to 'command not found' error

I'm facing issues with getting husky and lint-staged to function properly on my Windows 10 system. Here's how my setup looks like: .huskyrc.json { "hooks": { "pre-commit": "lint-staged" } } .lintstagedrc ( ...

Modify a variable within the service using multiple controllers

Seeking a more efficient way to accomplish the following: This is my service: dashboardApp.service('MessagesService', function () { var message = ""; this.update = function (new_message) { message = new_message; }; this ...

Differences between Array and Database Search

Currently, I have implemented a system where I store a refresh token in a JavaScript array as well as in each user's information table. When a user requests data, I first check the token in the array. If the token matches one in the array, I loop thro ...

Creating a live streaming application with Laravel and Latchet websocket technology

I'm currently developing a secure app that requires user authentication. However, I am facing challenges in identifying the authenticated user from my Latchet session. Due to Apache's lack of support for long-lived connections, I have hosted Latc ...

Problem with Jquery HTML CSS Slideshow

Currently I am working on integrating two sliders into one webpage using the demo available at . One of the sliders is customized from the Linking demo and I have named them slider1 and slider2 with different styling using global.css for slider-1 and text. ...

Issues with retrieving data using Angular's $http GET and jsonp

Encountering major challenges while attempting to connect to the Etsy API using both http and jsonp. The URL functions correctly in the browser: However, it returns a 404 error with jsonp and a 0 status error with $http.get I don't think CORS is th ...

Encountered 'fs: missing callback Error: EACCES' message while attempting to execute deployd application

Recently installed deployd version 0.6.10 using sudo npm install -g. However, encountering an issue when trying to run the app with the error message below; trying to start deployd v0.6.10... fs: missing callback Error: EACCES, open '/usr/local/lib/n ...

Sorting by numerical value in Angular

I have a JSON structure that looks like this - { "json" : [ { "200" : "5"}, { "302" : "6"}, { "400" : "1" } ] } My goal is to sort the data by the values in descending order, meaning I want the row with 6 to come first, then the ...

Unable to append additional data when making a jQuery ajax call with form data

Below is the script for my ajax call, in which I have included extra fields along with the form data. function paging(div_id, module, page_value) { var active_customer_list = $("#active_customer_list").val(); $("#"+div_id).html(loader); ...

Creating a "return" button for an editing form in AngularJS Datatables with the help of a form directive

Using AngularJS Datatables, I implemented a grid with additional "edit" and "delete" buttons in the last column. How is the grid/table rendered? HTML <!-- Required CSS and JS --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/li ...

Dividing an AngularJS module across multiple iFrames on a single webpage

Currently, I am working on a web application that consists of 1 module, 5 pages, and 5 controllers. Each HTML page declares the same ng-app. These pages are loaded within widgets on a web portal, meaning each page is loaded within an iFrame in the portal. ...

Trouble with Google Interactive Charts failing to load after UpdatePanel refresh

Desperately seeking assistance! I have spent countless hours researching this issue but have hit a dead end. My dilemma involves using the Google Interactive Charts API within an UpdatePanel to dynamically update data based on dropdown selection changes. H ...

Error: Discord Bot is unable to locate module './commands/ban.js'

Here is my code snippet: require('dotenv').config(); const fs = require('fs'); const Discord = require('discord.js'); const { error } = require('console'); const client = new Discord.Client(); client.commands = new D ...