Adding a Placeholder to an Input Field in AngularJS with an Active Class

My ng-repeat-generated list of inputs starts with only the first one enabled and styled with an active class (highlighted in red). You can see it in action on this example.

Whenever I focus on an input, the next one becomes enabled and also receives the active styling. This applies to all inputs dynamically.

My goal is to have a placeholder text visible only on inputs with the active class. Inputs without the active class should not have any placeholder text.

How can I achieve adding a placeholder dynamically to inputs with the active class?

Here's the relevant code snippet:

<div class="col-md-2-4 voffset3" ng-repeat="item in csTagGrp">
  <ul class="list-unstyled cs-tag-item-grp" ng-init="$parentIndex = $index">
     <li class="clearfix" ng-repeat="value in item.csTags">
        <div class="pull-left cs-tag-item-list">
           <input ng-focus="focusItem($index, $parentIndex)" ng-disabled="!value.active" ng-class='{active: value.active && !value.old}' type="text" class="form-control input-sm">
        </div>
       </li>
   </ul>
</div>

Appreciate any help in advance.

Answer №1

To make the input placeholder dynamic based on a condition, you can use the following approach. If the condition is true, set the placeholder to a value from the scope:

<input placeholder="{{value.active && !value.old ? placeholder : ''}}" ng-focus="focusItem($index, $parentIndex)"  ng-disabled="!value.active" ng-class='{active: value.active && !value.old}' type="text" class="form-control input-sm">

// controller
$scope.placeholder = "something";

Check out this plunker for an example.

Answer №2

If you want to create a function within the $scope that checks for the active status in your data model, you can follow this approach:

//controller
    $scope.getPlaceholder = function (item) {
     if(item.active){
       return item.tags;
     }
   }

//view
<input ng-focus="focusItem($index, $parentIndex)" 
           placeholder="{{getPlaceholder(value)}}"
           ng-disabled="!value.active" 
           ng-class='{active: value.active && !value.old}' 
           type="text" class="form-control input-sm">

I have made modifications to your Plunk as well.

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

Is there a way to achieve a similar effect to "object-fit: cover" for CylinderGeometry in three.js while utilizing THREE.VideoTexture()?

I'm really interested in creating something similar to this: https://i.sstatic.net/mWuDM.png Imagine the checkered background as a texture, and I want the jar to be a cylinder with that texture applied to it. My knowledge of three.js is limited, so ...

js include exclude switch a category

Although this question has been asked before, I have a query on completely removing an "id" from an element. Let's consider the following scenario: <div id="page"> some content here </div> I want to eliminate the "id" attribute from the ...

What is the best way to extract the JSON value from my API website and assign it to a new variable

Currently, I am in need of a way to convert a username into an ID. To accomplish this task, I will utilize the following API link: (where "username" is replaced by the variable name.) The main objective here is to extract the ID value from the provided li ...

Changing the static property of a React component does not trigger a re-render for every instance of that component

As a beginner in learning ReactJS, I recently delved into the concept of 'Modifying properties of components'. It was explained that when a property is modified, the 'render' method is called to reflect those changes. Curious, I wonder ...

Uh oh: encountered an unexpected token 'export' in node_modulesexpoAppEntry.js

I'm encountering a challenge with my Expo project. While attempting to launch my app using yarn start, I'm running into this error: Android Bundling failed 449ms error: node_modules\expo\AppEntry.js: Unexpected token 'export&apo ...

Choosing a key from Angular's controller

Here is the combo box I am working with: <select data-ng-model="data.selectedChannel" data-ng-options="salesChannel.value for salesChannel in salesChannels"> </select> In my controller, I have a function that calls a factory to retrieve a k ...

Load Form with Json Data from LocalStorage Key-ID to Auto-Populate Fields

After successfully setting the ID in localStorage, I now need to retrieve and display only that specific record from my JSON data. The content of my localStorage is: This information is obtained from my JSON data after a button click. The challenge is us ...

Configuring git npm dependencies to aid in debugging purposes

This is my first time trying to debug a library in my application and I'm not entirely sure how to go about it. I initially installed the library with npm install @react-pdf/renderer, but debugging was proving difficult. Then, I found a useful answer ...

Error Alert: Express configuration encounters Unforeseen character <

This is how I have set up my Express: const express = require('express'); const app = express(); app.use(express.static('public')); app.get('*', function (req, res) { res.sendfile('dist/index.html'); }); app.li ...

Confirming the existence of a folder with AngularJS

Currently, I am attempting to determine if a folder exists so that I can make decisions on which files to include using ng-include. This is what I have so far: $scope.isVisible = { buttons: checkForClientOverwride('buttons'), it ...

When deployed, Angular 14 and Bootsrap 5 are functioning properly on a local environment but encountering issues on an online

My Angular application (version 14.2.0) is integrated with bootstrap (version 5.2.3). Upon building and deploying the application on a local IIS server, everything displays correctly as shown in the image below: https://i.sstatic.net/pLDs6.jpg However, w ...

Divide the table row and store each cell using regular expressions

Here is the original source text: gi0/1 1G-Fiber -- -- -- -- Down -- -- Access gi0/2 1G-Fiber -- -- -- -- Down -- -- gi0/3 1G-Fiber -- -- -- -- Down -- -- gi0/4 1G-Fiber -- -- -- -- Down -- -- gi0/5 1G-Fiber -- -- -- -- Down -- -- gi0/0/1 1G-Fiber -- ...

I can't seem to get my closure to function properly

My goal is to avoid cluttering the global namespace with multiple variables, but since I am unable to utilize 'let' at the moment, I am resorting to creating closures. I have a basic webpage layout that includes a bootstrap accordion with each ca ...

The response from Ajax is not in object form

The output I received from my AJAX request is: ["1","O"] Though I need to extract the number 1 from it, when I use the code: console.log(result[0]); It returns: '[' Any suggestions on how to convert it to an object and retrieve only the f ...

The calculator is experiencing issues with JavaScript functionality

Having some trouble developing a calculator using HTML5, CSS, and JavaScript. After passing my HTML and CSS through validators successfully, I encountered issues when adding JavaScript functions to enable the functionality of the buttons on the calculator. ...

Accessing QML Functions from JavaScript

Currently, I am faced with a challenge in my app development using Qt. I need to trigger a QML function from JavaScript when a specific event is triggered from my HTML page. I attempted the following method: app.html <html> <head><title& ...

Having difficulty accessing information from Firebase database using the .once() function

When a button is clicked on the page, I need to fetch data from a Firebase database using the once() function. Despite setting up the necessary references and variables, the data retrieval seems to be unsuccessful as the global variable numElections keeps ...

How to dynamically generate data with exceljs

Currently, I am attempting to download JSON data into an Excel spreadsheet. One of my requirements is to insert an image in cell a1 and then bold the headers. The code snippet below was sourced from Google; however, I need to populate the data dynamically ...

Limit an object to only contain interface properties

Suppose we have the following object: o {a : 1, b : 2} and this interface defined as: interface MyInterface { a : number } We are now looking to create a new object that represents the "intersection" of o and the MyInterface: o2 : {a : 1} The mai ...

What method can I use in Javascript to extract the mealtype data from the Edamam nutritional API?

My goal is to extract the mealtype information from the API linked below. After reviewing the API, it seems that a POST request is required to retrieve mealType data. However, I am uncertain about the correct syntax for this request. For instance, if a u ...