Tips for accessing the return value in Ionic 2

Having issues with getting the returned value of one function to work in another. The returned value always comes back as undefined.

Here's the code I'm using for returning values:

export class HomePage {

  variables={
    a:1,
    b:2,
    c:3,
  }

  constructor(public navCtrl: NavController) {
     this.runFunction1();
  }

  runFunction1(){
     if(this.checkFunction2()){
        alert("true");
     }else{
        alert("false");
     }
  }

  checkFunction2(){
     if(this.variables.a + this.variables.b >= this.variables.c){
        return true;
     }else{
        return false;
     }
  }
}

Answer №1

When both functions are declared as part of the component, it is necessary to utilize the this keyword in order to run them:

 function1(){
     if(this.function2()){ // <--- this way!
        alert("true");
     }else{
        alert("false");
     }
  }

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

Improving JavaScript Functions: Minimize duplication of helper methods

I have a set of helper functions that check for the presence of specific strings in an array and certain steps before triggering other functions. The reason for keeping them separated is because arrTours must be associated with only those arrSteps. // Help ...

What is the method for adding a document within an array that is nested within another document?

Apologies if the title seems complex... I struggled to find a better way to describe it. The scenario I am dealing with fits the following Schemes: Collection1: const mongoose = require('mongoose'); const itemSchema = mongoose.Schema({ _id: ...

Encountering a 404 error with Angular 2 Jasmine XHR while initializing a constructor

I have been diligently following the official Angular 2 tutorials on setting up unit testing using Jasmine. My goal is to mock a Profile object that is defined in another class. However, whenever I attempt to instantiate Profile using its constructor, an e ...

Utilizing Services in Angular 2

I am new to learning angular2 and encountering an issue with calling a service. The service call is successful, but I am unsure how to handle the data. In Angular1, we would handle it like this: DemoService.getExchangeDataConnection().then(function(respon ...

Using Typescript to pass an optional parameter in a function

In my request function, I have the ability to accept a parameter for filtering, which is optional. An example of passing something to my function would be: myFunc({id: 123}) Within the function itself, I've implemented this constructor: const myFunc ...

Angular2's AngularFire2: Issue with Nested Observables not Displaying in the View

I am currently working on an experimental app using Ionic 2, Firebase, and AngularFire2 (which is still in alpha). I have been following a tutorial by Aaron Saunders as a foundation for my project: https://github.com/aaronksaunders/ionic2-angularfire-sam ...

What is the easiest method to conceal the initial button within a collection of buttons in an Angular2 template?

https://i.sstatic.net/j7XnN.png Within this section, I have a series of buttons, and I am looking to hide the first button in the list. What is the best way to accomplish this? Here is the HTML template I am working with: <section class="list-group ...

Is it possible to verify a file's type with Nestjs Pipes and the FileTypeValidator?

I am facing an issue with implementing a Nestjs route in a controller that includes a file upload using Multer. The goal is to edit a user's profile picture, so I need to validate that the uploaded file is an image. However, despite using the FileType ...

The array is not being spliced in the DOM, however, it is being spliced in the console - Ionic 2+/Angular

My scenario involves a dynamic array filled with items and values. The goal is to remove an item from the view list when a user clicks a button on that particular item. I'm struggling to identify why this functionality isn't working as expected. ...

Merge two input fields into one to send data to the backend

I have created two input fields, one for selecting a date and the other for entering a time. Before submitting the form to the backend, I need to combine these two inputs into one variable. For example, <input type="text" name="myDate"> and <input ...

What is the significance of parentheses when used in a type definition?

The index.d.ts file in React contains an interface definition that includes the following code snippet. Can you explain the significance of the third line shown below? (props: P & { children?: ReactNode }, context?: any): ReactElement<any> | nu ...

How to arrange table data in Angular based on th values?

I need to organize data in a table using <th> tags for alignment purposes. Currently, I am utilizing the ng-zorro table, but standard HTML tags can also be used. The data obtained from the server (via C# web API) is structured like this: [ { ...

How to modify CSS style in SVG using Angular2?

I have been working on adding SVG to an Angular2 component's template and I've encountered some challenges. Here is the code snippet I am using: <object type="image/svg+xml" data="kiwi.svg" class="logo"> Kiwi Logo </object> To dyn ...

A function that logs a message to the console if an array contains identical values

Struggling to find equal values in my array, I've attempted several methods without success. One approach I tried involved sorting the array: var sorted_arr = this.variacaoForm.value.variacoes.sort(); // the comparing function here for (var i = 0; ...

Changing the generic type's constraint type in TypeScript to have more flexibility

I have developed a utility type named DataType that takes in a parameter T restricted to the type keyof MyObject. When the key exists in MyObject, DataType will return the property type from MyObject; otherwise, it will simply return T. interface MyObject ...

Lint error: Expression detected when an assignment or function call was expected according to @typescript-eslint/no-unused-expressions rule

Encountering an error message while utilizing the code snippet countryMap.get(tradeId)?.map((companies) => {: The error states 'Expected an assignment or function call and instead saw an expression @typescript-eslint/no-unused-expressions' con ...

Typescript is throwing a fit because it doesn't like the type being used

Here is a snippet of code that I am working with: import { GraphQLNonNull, GraphQLString, GraphQLList, GraphQLInt } from 'graphql'; import systemType from './type'; import { resolver } from 'graphql-sequelize'; let a = ({Sy ...

Ways to implement pointer event styling within a span element

Hi, I'm having trouble with styling and I can't seem to figure out how to resolve it. The style pointer-events: none doesn't seem to be working for me. Here is an example of my code: The style snippet: .noclick { cursor: default; ...

Explore the capabilities of dart:io library to open and read files in Dart 2

Looking for help with reading a file in Dart. I'm new to the Dart language and I'm trying to read a file (test.txt) using the dart:io library. I've been following this link for guidance: However, despite my attempts, I keep encountering an ...

Error: The global variable "Stripe" has not been defined

Having trouble with integrating the Stripe API. Development Environment: "devDependencies": { "@angular-devkit/build-angular": "~0.803.20", "@angular/cli": "~8.3.23", "@angular/compiler": "~8.2.14", "@angular/compiler-cli": "~8.2.14", ...