Harnessing the power of dynamic scope variables within an ng-if statement

I am trying to implement ng-if directives based on the values of "male", "female" or "all". However, when using data-ng-show, the values are working fine as true and false for AnswerList2[index][GenderTypeAnswer]. I want to achieve this without displaying the element in the DOM.

var sampleData=[{
        "QuestionId": 1,
        "Question": "Please tick the box that applies to you",
        "AnswerList": [
            {
                "AnswerId": 1,
                "Answer": "Male",
                "GenderTypeAnswer": "male"
            },
            {
                "AnswerId": 2,
                "Answer": "Female",
                "GenderTypeAnswer": "female"
            },
            {
                "AnswerId": 3,
                "Answer": "NA",
                "GenderTypeAnswer": "all"
            }
        ]
    }];
     $scope.all=true;
     $scope.male=true; 
     $scope.female=false; 
     
     
     
<p data-ng-repeat="(index,value) in sampleData[0]['AnswerList'] as AnswerList2" data-ng-if="AnswerList2[index][GenderTypeAnswer]" data-ng-bind-html="AnswerList2[index]['Answer']"></p>

I have a JSON object with "GenderTypeAnswer" key values that I'm using as scope variables in my JavaScript. However, I'm facing issues retrieving these values in the ng-if directive. Can anyone provide assistance?

That's the crux of it

The value of AnswerListLast[index]['GenderTypeAnswer'] is appearing as-is in ng-if.

Answer №1

Make sure to check if AnswerListLast[index]['GenderTypeAnswer'] has a value before using ng-if, consider trying the regular ng-if instead of data-ng-if... Check out this interesting discussion on Stack Overflow regarding why ng-if="true" + data-ng-if="true" evaluates to 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

Tips for including extra items in a JSON String using Angular 2

function execute(req:any): any { var stReq = JSON.stringify(req); // Adding additional item "Cityname": "angular2City" inside req req.Cityname = 'angular2City'; } Now, how can I include the additional item "Cityname": "angular2C ...

Guidelines for accurately and appropriately creating divisions in JavaScript using mathematical principles

The program occasionally generates mathematically incorrect divisions, such as 2/0, and does not accept rounded answers like 0.33 or 0.33333 for "1/3". What modifications should I make to the code? Thank you for your assistance and time. document.getEl ...

How can I display only the y-axis values and hide the default y-axis line in react-chartjs-2?

Although I have some experience with chartjs, I am struggling to figure out how to hide the default line. To clarify, I have attached an image that illustrates the issue. I would like to achieve a result similar to this example: https://i.sstatic.net/UXMpi ...

Strategies for bypassing Jquery form validation within a single form element

I want to implement form validation for all form elements, but I need to exempt one if a specific checkbox is checked. HTML <form id="reg_form"> <div class="control-group"> <div class="control"& ...

Is using the bind operator the key to currying a function?

I have discovered that this.handler.bind(this) can be substituted with ::this.handler. How can I go about replacing this.handler.bind(this, 1) using only the :: operator? This technique came in handy for me when I needed to add handlers to react component ...

Animation that responds to scrolling movements

Is there a way to animate text based on scrolling? <p class="class">TEXT</p> transform:translateX(-500px);opacity:0; transform:translateX(0px);opacity:1; ...

Ensure the scope values are consistent following the call to location.path()

When a button is clicked, I want to change the template using the same model value. I am utilizing location.path to switch the template. Plunk- http://plnkr.co/edit/FVkSj2vs8WDAt1eifBpF?p=preview app.js var app = angular.module('includeExample&apos ...

Troubleshooting a 400 error with Express and React: A step-by-step guide

When I check the browser console, I see this error message: `xhr.js:251 POST http://localhost:325/budget 400 (Bad Request) BudgetNew.js:30 catch AxiosError {message: 'Request failed with status code 400', name: 'AxiosError', code: ...

The ng-repeat directive is limited to functionality only within parent divs that have the id of

Is there a way to make ng-repeat work only in one specific place by defining an id where it should apply? I would like to achieve this functionality from the controller. For example, using something like $(#1).(ng-repeat work) Here is an example: <t ...

Convergence of two textures within the same location in three.js

I would like to layer two different materials on a plane - a brick background and another material with a distinct texture on top. ...

The POST request in node.js is failing to post data to the database and is instead receiving a 302

Recently, I've been tinkering with a basic CRUD app using node.js, express, and mongodb. Following various tutorials, I attempted to set it up on my own. However, I'm encountering an issue with a 302 response on my POST request. router.route(&ap ...

The button's URL will vary depending on the condition

As a newcomer to coding, I am seeking guidance on creating a button with dynamic URLs based on user input. For instance, let's say the button is labeled "Book Now" and offers two package options: Standard and Premium. I envision that if a user selec ...

Is it possible to utilize window.location.replace function within an iframe?

Is it possible to use window.location.replace to bypass history and target on-page anchors without page reloads, but encounter issues when working within iframes? The main problem seems to be a CSP (content security policy) violation for script-src ' ...

Dynamic elements fail to trigger JS function calls

I have a <div> that I dynamically populate with <input> and <a> elements using jQuery. It looks like this: The <div> gets filled when the addServicePanel() function is called: function addServicePanel() { var wrapper = $(".m ...

The sorting icon cannot be substituted with jQuery, AJAX, or PHP

Currently, I am working on implementing "sort tables" using ajax, jquery, and PHP. The sorting function is functioning correctly; however, I need to show/hide the "sorting images". At the moment, only one-sided (descending) sorting is operational because I ...

NextJs not processing Bootstrap form submissions

I’m struggling to figure out why my form isn’t submitting when I click the submit button. The backend seems fine because Postman successfully sends the information to the database. However, nothing happens when I try to submit the form. My tech stack ...

In what ways can I leverage the functionalities of an AngularJS directive to delay the display of content until a user clicks on it?

Our rental application utilizes an API call to populate an array, triggering an ngRepeat and generating a list of divs displaying basic rental property information. Upon clicking a specific property, another API call is made to populate an interior ngRepe ...

Challenge encountered while attempting to implement grid system in ionic framework

I'm completely new to the world of ionic framework, and I really need some assistance in completing this view. Please see the image reference https://i.stack.imgur.com/WOBFw.png I have made an attempt at it with the following code: <div class ...

`Issue with multiple replacevariablebyhtml functions not functioning properly within PHPDocX`

When attempting to replace multiple variables in a document using the functions replaceVariableByHTML and replaceVariableByText, I encountered an issue. If I only use replaceVariableByText, everything works fine. However, when I add HTML replacement, the ...

NodeJS has a knack for replying even before the function has completed

Struggling with a NodeJS and Express API for a school project. The getAuthUserId function is not working as expected. It decodes the JWT token to retrieve the user Id from the mongoDB server. However, when calling this function in a REST call "/user/authT ...