Ensure that you provide distinct values for both the model and view in your input

Is there a way to automatically format the numbers I type with a $ symbol in the input field, without actually storing the $ in the model? I've tried different methods but so far, if there is already a value in the model (not null), it displays with $. However, if the input is empty or I delete everything and start over, the values are not formatted. What am I missing here? Check out this jsfiddle: http://jsfiddle.net/rmzqomzz/1/

angular.module('HelloApp', []);

function myTest() {

 return {
  restrict: 'EA',
  require: 'ngModel',
  link: function(scope, element, iAttrs, ngModel) {
      console.log(iAttrs.myTest);
      ngModel.$formatters.push(function(modelValue){
          if(!modelValue) return;
          if(iAttrs.myTest == 'money'){
              return '$ '+modelValue;
          }else{
              return modelValue +' %';
          }
      });
      ngModel.$parsers.push(function(viewValue){
          return viewValue.replace('$', '').trim();
      });
      scope.$watch(
          function(){
              return ngModel.$modelValue;
          }, function(newValue, oldValue){
              console.log('value changed '+ newValue);        
          }, true
      );
  
  }
 };
};

angular.module('HelloApp').directive('myTest', myTest);

angular.module('HelloApp').controller('HelloController', function($scope) {
$scope.mon = {'balu':null};
});

Thanks

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

Issue encountered while trying to pass updated values from an Angular UI modal back to the parent screen

When I click on an item in a list, I want to open an angularUI modal. I can successfully load the modal, make changes, and return to the main screen. However, I am struggling with updating the list on the main screen after closing the modal. I've trie ...

Dynamic image swapping using JSON key in Angular

Trying to display different icons based on a property that contains specific words. If the property includes "Health Care" or "Health Care .....", I want to show one image, otherwise another. I've tried using ng-show but my syntax seems off. Any sugge ...

Learn how to effectively utilize templateURL in an express and angular project

Our project utilizes Express without any view engine. To set up static directories, we have the following: app.use(express.static(__dirname + '/public')); app.use(express.static(__dirname + '/view')); app.use(express.static(__dirname + ...

Tips for implementing a DatePicker in JHipster's UI

Looking to enhance the functionality of a JHipster entity page by integrating the DatePicker. I've already included the necessary dependencies via Bower and added the JS library URL to the index page. However, I'm uncertain about how to incorpora ...

Creating a custom Angular filter that leverages the power of $http - a

I want to make a retrieval request using $http in AngularJS and then display the obtained result on the front-end. To achieve this, I'm using {{ URL-STRING | iframely }}. 'use strict' angular.module( 'iframely', [] ).filter( &ap ...

What is the formal designation for the 'http://localhost/index.html' section within the url 'http://localhost/index.html?v1=v'?

As I strive for good naming practices, I am currently pondering what to label the protocol + host + port + path segment of a URL. Working with AngularJS, my aim is to transmit this information from my Single Page Application (SPA) to the server for email v ...

Implementing script loading within the Angular scope

I'm attempting to load a custom script from the database based on client-side logic. I am having trouble figuring out how to make it function properly. Here is my controller code: 'use strict'; angular.module('youshareApp') . ...

Node API is failing to insert user data into MongoDB

I'm currently developing a Restful API using Node.js and storing data in Mongodb, focusing on the user registration API. app.js apiRoutes.post('/signup', function(req, res) { if (!req.body.name || !req.body.password) { res.json({suc ...

Utilizing Typescript to implement an interface's properties

After declaring an interface as shown below interface Base { required: string; } I proceeded to implement the interface in a class like this class MyClass implements Base{ method(): void { console.log(this.required); } } However, I ...

The element event does not trigger an update on the view

I am trying to display the caret position of my editor on a specific place on the website. I have created a directive and service to share variables between the controller and directive. Inside the directive, I have enabled events like "keyup", "mouseup", ...

Leverage the greater than operator within an AngularJS controller

This is the data stored in my controller: $scope.data = [{ "F": "1,26,000" }]; $scope.data2 = [{ "F": "26,000" }]; Now I want to implement an if-else condition using this data: if (Number($scope.d ...

Utilizing $asyncValidators in angularjs to implement error messages in the HTML: A guide

This is my first major form with validations and more. I've set up a Registration form and I'm utilizing ng-messages for validation. The issue arises when I have to check the username, whether it already exists in the JSON server we are using or ...

Ng-repeat seems to be having trouble showing the JSON data

Thank you in advance for any assistance. I have a factory in my application that utilizes a post method to retrieve data from a C# function. Despite successfully receiving the data and logging it to the console, I am facing difficulties in properly display ...

Error: AngularJS is experiencing an injector module error that has not been caught

I have set up an Angular boilerplate that includes various elements such as meta tags, CDN links, and script tags. However, I am encountering a persistent error in my console and cannot figure out the root cause of it. https://i.stack.imgur.com/qPGby.png ...

Using various designs on elements generated from ng-repeat

Is it possible to apply different styles to buttons created using ng-repeat by having b.color return a value from the btnValues object? Currently, it is not returning the value. If this is not possible, are there alternative methods to achieve this? < ...

JavaScript: set values to elements in an array

Below is the code snippet I am working with: function gotData(data){ result = data.val() const urls_kws = Object.keys(result) .filter(key => result[key].last_res > 10) var keywords = urls_kws; c ...

Show labels for data on a circular graph using angular-chart.js

I recently created a pie chart using angular-chart.js and it's functioning smoothly. However, I'm facing an issue with displaying the data value on each section of the pie chart. My attempt to use Chart.PieceLabel.js by adding the code snippet b ...

Issue with AngularJS UI Router not loading the inline template and controller

I am trying out UI Router for the first time in my AngularJS project. I am facing an issue where, when I click on a link to view a post, it doesn't display. The post template is not visible and I remain on the home page. The URL flashes as http://loc ...

Looking to convert AngularJS data from JSON format to a more standard format?

What is the typical way this will operate? Hello, I have developed a basic REST service: @GET @Path("/SayHello") @Produces(MediaType.APPLICATION_JSON) public String sayhello(){ String name="Hello"; return name; } I am accessing it using $ ...

When sending data to the server, Braintree's payment_method_nonse is found to be void

I'm currently in the process of configuring DROP-IN UI. My backend setup involves Laravel 5.2 (following the instructions provided in the documentation: [https://laravel.com/docs/5.2/billing#braintree-configuration] I have put together a BraintreeCo ...