Tips for modifying the max length value in Angular

I'm just starting out with Angular and I'm trying to alter the maxlength property from 300 to 140 upon clicking a button. The buttons are generated using ng-repeat and only the first one should change the value to 140, while the others should remain at 300.

Here is the code snippet from my controller:

//character counter
$scope.counter = function() {
    var element = angular.element(document.querySelector('.form-control'));
    element.attr('maxlength', '150');
};

And this is the relevant HTML:

<textarea data-ng-model="view.post.content" ng-trim="false" maxlength="340" class="form-control" style="height: 100px;"></textarea>

Answer №1

Utilize the ng-maxlength directive and bind it to a property in the scope for validation security.

For example:

<textarea data-ng-model="view.post.content" ng-trim="false" 
            ng-maxlength="maxValue" class="form-control" 
            style="height: 100px;"></textarea>

If you want to strictly restrict input length, use interpolation like this: maxlength={{maxValue}}.

<textarea data-ng-model="view.post.content" ng-trim="false" 
            maxlength="{{maxValue}}" class="form-control" 
            style="height: 100px;"></textarea>

Start with $scope.maxValue = 340 then update the value of the property inside the counter to 150.

Documentation

ngMaxlength (optional) number : Sets the key for the maxlength validation error if the value exceeds maxlength. Using a negative or non-numeric value allows values of any length to be viewed.

Example

Answer №2

Utilize the angular directive ng-maxlength for character limit

<textarea data-ng-model="view.post.content" ng-trim="false" ng-maxlength="{{len}}" class="form-control" style="height: 100px;"></textarea>

In your controller

//character counter
$scope.len = 300;
$scope.counter = function() {
  $scope.len = 150;
};

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

A guide to mastering Controllers in AngularJS

Trying to set up a basic page with a controller but encountering difficulties. The HTML code is straightforward, with an Angular script included, but the functionality isn't working as expected. The HTML snippet: <!DOCTYPE html> <html ng-ap ...

Is AngularJS limited to animating in just one direction?

Reordering 5 items in an array has led to a discovery - when shuffled, only the items with higher indexes have smooth animations while others simply jump to their new positions. <div ng-repeat="item in items track by item.value" class="TestItem" ng-sty ...

Establishing bidirectional communication with a service property in an Angular application

A service called settings is being used to store application-wide settings as public variables. The goal is to allow users to modify these settings through different input elements, which requires establishing a "two-way binding" between a property of the ...

Is there a way to replicate Twitter's "what's happening" box on our website?

Currently, I am trying to extract the cursor position from a content-editable box. However, when a new tag is created, the cursor appears before the tag instead of after it. Additionally, I am having trouble with merging/splitting the tags. Any suggestions ...

I'm having trouble with res.redirect, why isn't it redirecting me as expected?

In my login controller, I have a form that updates the user's scope when they click a button triggering the login() function. .controller('loginCtrl', ['$scope','$http',function($scope,$http) { $scope.user = { ...

The functionalities of $scope and this in AngularJS

Currently, I am developing a small application using angularjs. In this project, I am trying to implement a feature that involves deleting a contact. The functionality itself works perfectly fine, however, I am encountering an issue where the 'this.op ...

preserving the factory variable when reloading the page

I am faced with a challenge in my AngularJS website where I have two views - one for a filtered list of posts and another for a specific post. Each view has its own controller, and I need to pass the specified category and page information to the post-spec ...

Exploring Angular: Why is the link option important in directives?

As I dive into learning Angular, I often find myself struggling to grasp the code and its significance. Take a look at the example below and help me understand the purpose of the link option in directives. What exactly does the link option do? When does t ...

What could be causing a template value in my AngularJS/Ionic Framework code to not be replaced properly?

Recently, I've been exploring the world of Ionic Framework with Angular by my side. However, I've hit a bit of a roadblock. My goal is to set up tabs at the bottom of my application using a model definition. Here's what I've tried so ...

Tips for maintaining the data on a page continuously updating in AngularJS

I have this code snippet: $cookieStore.put('profileData', $scope.profileData); var profileData = $cookieStore.get('profileData'); $scope.init = function(){ var profileData = $cookieStore.get('pr ...

The ng-repeat function is not functioning properly when used within an li element to trigger

I have utilized the Dialog service to create a pop-up window. My intention is to display a message to the user in this format: txt = '<ul> <li data-ng-repeat = "eachValue in dummnyList" > {{eachValue | applyFilte ...

Utilizing AngularJS: Executing directives manually

As a newcomer to AngularJS, I am facing a challenge that requires creating a 3-step workflow: The initial step involves calling a web service that provides a list of strings like ["apple", "banana", "orange"]. Upon receiving this response, I must encap ...

AngularJS Firebase Login Scope Value Not Retained After Refresh

I have stored my unique username in the variable "$scope" and I am trying to access it from different views once the user logs in, using: However, when I refresh the page immediately after the user successfully signs in, the value of "$scope" goes bac ...

Using AngularJS to Filter Array Elements

Here is a sample of the JSON data I am working with: products: [{ id: "1", model: "123", price: "123", spec: "213", image: "", brand: "213", category: "1", sub_category: "1", color: "1", size: "1", order: "1", ...

Inject the variable into location.href

I am dealing with a dynamic URL, and I want to achieve the following in my HTML: <a onclick="location.href='myVariable';"><span>Click here</span></a> The issue is that this approach displays the actual string 'myVar ...

Tips for passing dynamic latitude and longitude values to a JavaScript function within an AngularJS framework

I am facing an issue with displaying a dynamic marker on Google Maps. I can show a static lat long marker, but I am struggling to pass dynamic lat long values to the function. How can I pass {{names.lat}}, {{names.longitude}} to the function so that I can ...

Rendering nested rows in Angular datatables

How can nested tables be better displayed in angular-datatables? One solution involves using rowCallback and setting up click events: $scope.dtOptions = DTOptionsBuilder.fromSource('data.json') .withOption('rowCallback', rowCallbac ...

I am encountering an unresolved error: [$injector:modulerr] Problem on my AngularJS application related to routing

Attempting to develop an angular application, encountering the following error: The issue is outlined in this link: http://errors.angularjs.org/1.3.13/$injector/modulerr?p0=app&p1=Error%3A%20…ttp%3A%2F%2Flocalhost%3A8888%2FHotelAdmin%2Fjs%2Fangular. ...

Tips for resetting the ngrepeat index within a modal

I have a group of elements, each with its own attached gallery. I've implemented a modal to display the gallery for each element, and it's functioning flawlessly. However, I'm encountering an issue with the initial pictures that are display ...

Upon reviewing the webpage using an IPAD and AngularJS

I recently completed a web application using AngularJS and PHP. It functions smoothly on Chrome and Firefox, but it encounters loading issues on IE due to the number of JS files. To solve this problem, I will need to reduce the amount of JS files for it to ...