Verify the ng-if condition for a specific value and display an alternative option if the condition is not

When obtaining a response from the server in JSON format (containing color.mix and color.pure), it is passed directly to the template. In this template, I need to display a value if it exists or show another value if it does not.

<span ng-if="color.mix">{{color.mix}}</span>

If the value is not present, I want to display the 'pure' value. Initially, I attempted the following:

<span ng-if="!color.mix">{{color.pure}}</span>
<span ng-if="color.mix">{{color.mix}}</span>

However, when both values are received, both are displayed simultaneously.

What would be the correct approach to achieve this?

(Using Angular 1.6)

Answer №1

<span>{{color.combined ? color.combined : color.pure}}</span>

Answer №2

Ensure that both conditions are checked

<span ng-if="!color.mix && color.pure">{{color.pure}}</span>
<span ng-if="color.mix && !color.pure">{{color.mix}}</span>

If you prefer a one-liner using a ternary operator, you can simply write,

<span>{{color.mix ? color.mix : color.pure}}</span>

DEMO

var app = angular.module('todoApp', []);
app.controller("dobController", ["$scope",
  function($scope) {
    $scope.color = {
      pure: "test"
    };
  }
]) ;
<!DOCTYPE html>
<html ng-app="todoApp">
<head>
  <title>To Do List</title>
  <link href="skeleton.css" rel="stylesheet" />
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
</head>
<body ng-controller="dobController">
<span>{{color.mix ? color.mix : color.pure}}</span>
</body>
</html>

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

Error: Oops! The super expression can't be anything other than null or a function in JavaScript/TypeScript

I am facing an issue with class inheritance in my code. I have a class A that extends class B, which in turn extends class C. Whenever I try to create a new instance of class A within a function, I encounter the following error message: Uncaught TypeError: ...

AngularJS: Starting a $scope property in the right way

Check out my small angular code snippet below: html file: <body ng-controller="myCtrl"> <div class="container"> .... </div> </body> javascript file: app.controller('myCtrl', function($scope) { $scope.wh ...

Selecting an option from the dropdown menu to automatically fill in a textbox within

I've run into a small hiccup with some javascripts/AJAX and could really use some guidance in the right direction. My issue involves populating the per-carton-price-field using collection_select within a form. This form is meant to generate an entry ...

Retrieve the id of the anchor tag that was selected upon clicking, and then dynamically change the content of another div

Seeking guidance on how to dynamically change the content of a div element based on which anchor tag is clicked. Below is the HTML structure and JavaScript function I have attempted: HTML: <div> <a id="a1" href="javascript: changeDiv();">tag1 ...

"Utilizing ng class with an array of objects: A step-by-step guide

I am facing a scenario in which my response appears as follows: "currency" : [ { "_id" : ObjectId("584aad5d3e2537613e5f4c39"), "name" : "USD" } ], I need to enable my checkbox based on the currency name. I attempted the followi ...

What is the best way to create a drop-down menu that exports data directly to an Excel spreadsheet?

I have been struggling with a seemingly simple issue - I can't seem to get my drop-down box to display the chosen option. The code I'm using is quite similar to this generic one, but for some reason, it's not reporting the selected option to ...

How can I trigger a series of functions in sequence when a button is clicked using Vue.js?

When I click, I need to call 3 functions in a specific order: <CButton @click=" getBillPrice(); updateBill(); postData();" type="submit" color="primary">Save</CButton> However, the func ...

Calculating the sum() of a specific attribute in Loopback without the need to iterate through every instance of the object

Within my DummyModel, there are attributes named attOne, attTwo, and attThree. If we want to retrieve all instances of attOne, we can utilize the following query: DummyModel.find({where: {attTwo: 'someValue'}, fields: {attOne: true} }); The ab ...

Storing a JSON response in a variable

I need assistance with using the Google Geocoder API to obtain the longitude and latitude of an address for a mapping application. How can I retrieve data from a geocode request, like this example: "http://maps.googleapis.com/maps/api/geocode/json?address ...

Roundabout Navigation Styles with CSS and jQuery

My goal is to implement a corner circle menu, similar to the one shown in the image below: https://i.stack.imgur.com/zma5U.png This is what I have accomplished so far: $(".menu").click(function(){ $(".menu-items").fadeToggle(); }); html,body{ colo ...

Having trouble retrieving mobiscroll instance in Angular with Ionic

I'm facing an issue with accessing the instance of my mobiscroll widget in Angular 4 with Ionic Framework. Despite following all the correct steps, the actual widget won't get selected. Below is the code for the widget: <mbsc-widget [options ...

How can I differentiate between server-side and client-side JavaScript in Node/Express?

I decided to challenge myself by working on a JavaScript project to enhance my skills, but I am facing some difficulties distinguishing between the client-side and server-side code. Currently, the setup involves a Node app with ExpressJS as a dependency. ...

The FetchStrategy.FromLocalCache in BreezeJS isn't working as expected and not returning

Something strange is going on here, as I am no longer getting results from the local cache using fetchStrategy. This situation doesn't seem to make sense. Could it be a bug of some sort? Even though the entities are in the cache after the initial qu ...

Using Regular Expressions in an ExpressJS Router

When working with ExpressJS, is there a way to combine the following routes into one using RegEx? app.get(/^\/blog(?:\/p(\/\d+)?)?$/, blog.list); ...

React - Dealing with rendering issue when toggling a button using LocalStorage and State

For quite some time now, I've been struggling with a particular issue... I'm encountering challenges in using the current state to display a toggle button that can both add and remove an item from local storage. The code below manages to add and ...

Challenges encountered when redirecting users with a combination of javascript and php

I have a login form that triggers a JavaScript function upon submission. This function calls a PHP page to process the input. The issue I'm facing is with how the redirections are displayed based on the user's role type. It attempts to display t ...

Steps for rearranging the order of CSS grid layout

Could you assist me in implementing a grid layout that fulfills these specific criteria? I'm unsure of how to proceed, so any guidance would be greatly appreciated. When there are more than 9 items, the current layout changes the order from top to bo ...

Implementation of the I18next library

I am currently integrating react-i18next into my application to implement a switch button for French and English languages. Unfortunately, I am facing some issues as the translation is not working properly. I suspect it's related to the JSON file reco ...

Is the neglected property being discarded?

First things first, let's talk about my class: class FavoriteFooBar { ... isPreferred: boolean = false; constructor() { this.isPreferred = false; } } Using a utility library called Uniquer, I arrange a list of FavoriteFooBar instances to pr ...

Angular Promises - Going from the triumph to the disappointment callback?

It seems like I might be pushing the boundaries of what Promises were intended for, but nonetheless, here is what I am attempting to do: $http.get('/api/endpoint/PlanA.json').then( function success( response ) { if ( response.data.is ...