Is there a way to swap out multiple characters within a string when using ng-repeat in AngularJS?

How can I replace multiple characters in a string using ng-repeat in AngularJS?

I've tried the following code, but it's not working. I need to remove #, _, and . from the strings in my list. How can I achieve this in AngularJS?

<body>

<div ng-app="myApp" ng-controller="namesCtrl">

<p>Type a letter in the input field:</p>

<p><input type="text" ng-model="test"></p>

<ul>
  <li type="1" ng-repeat="x in names | filter:test">
    {{ x.replace(/#|_/./g,'') }}
  </li>
</ul>

</div>

<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {

    $scope.names = [
        "Jana#ai.mkv",
        'Car.l.mkv',
        'Mar##gareth.mkv',
        'Hege.mkv',
        'Jo_e.mkv',
        'G__ustav.mkv',
        'Birgit.mkv',
        'Mary.mkv',
        'Kai.mkv'
    ];
});
</script>



</body>

Answer №1

To replace the # inside a function, you can create a function that applies the necessary logic.

EXAMPLE

angular.module('myApp', []).controller('namesCtrl', function($scope) {
   $scope.getText = function(obj){
    return obj.replace(/#|_|\./g, '');
  };
    $scope.names = [
        "Jana#ai.mkv",
        'Car.l.mkv',
        'Mar##gareth.mkv',
        'Hege.mkv',
        'Jo_e.mkv',
        'G__ustav.mkv',
        'Birgit.mkv',
        'Mary.mkv',
        'Kai.mkv'
    ];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="namesCtrl">
<p>Type a letter in the input field:</p>
<p><input type="text" ng-model="test"></p>
<ul>
  <li type="1" ng-repeat="x in names | filter:test">
    {{ getText(x)  }}
  </li>
</ul>

</div>

Answer №2

Make sure to fix your regex first. It seems like it should be

/#|_|\./g

Create a function within the controller and utilize it in the mustache template. For example:

angular.module('App', [])
  .controller('Ctrl', function() {
    var vm = this;

    vm.names = [
      "Jana#ai.mkv",
      'Car.l.mkv',
      'Mar##gareth.mkv',
      'Hege.mkv',
      'Jo_e.mkv',
      'G__ustav.mkv',
      'Birgit.mkv',
      'Mary.mkv',
      'Kai.mkv'
    ];

    vm.replace = function(value) {
      return value.replace(/#|_|\./g, '');
    };
  });
<div ng-app="App" ng-controller="Ctrl as vm">
  <p>Enter a letter in the input field:</p>
  <p><input type="text" ng-model="test"></p>
  <ul>
    <li ng-repeat="x in vm.names | filter: test">
      {{ vm.replace(x) }}
    </li>
  </ul>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script>

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

unable to retrieve the value from the scope

I'm trying to implement the following code in HTML: <div ng-if="item.shareText"> <textarea></textarea> <div> <select ng-model="shareOptions"> <option value="PUBLIC" selected>public</option> ...

Guide on creating a conditional column in a kendo UI grid with the use of AngularJS

Is there a way to dynamically add a column to the Kendo UI grid based on JSON data? Consider the following JSON input: [{ "ProductID": 1, "ProductName": "Chai", "Supplier": { "SupplierID": 1, "SupplierName": "Exotic Liquid ...

Enhance filtering capabilities in ng-repeat by adding controls

After utilizing ng-repeat to display some JSON data, I have incorporated form controls to filter output data from another JSON file. Here is a simplified example: First JSON data: var technologies = [ {"id":"5", "slug":"mysql", "label":"MyS ...

Adding Jade template variable to an inline function

Trying to implement the JSCharts library. block content | <div id="chartcontainer">This is just a replacement in case Javascript is not available or used for SEO purposes</div> script. var myData=new Array() var myData = new Array([10 ...

When using NodeJS, having multiple 'if' statements may result in conflicting headers being returned,

Introduction to Promises. Encountering challenges in NodeJS due to the utilization of multiple if-statements and return-statements. Considering leveraging Promise as a potential solution. This snippet showcases an example: const express = require(' ...

Connecting different jQuery methods to create a chain

From my understanding of jQuery chaining, the first method in the list must complete before the next method executes. Here is an example: $.fn.reportZebraStriper = function(options) { console.log('reportZebraStriper()'); return true; } ...

Tips for including assisting text in the date field with Material UI

Behold, my component definition: <DateField name={formElement.name} type="date" label={formElement.name} onChange={(date) => formik.setFieldValue(formElement.name, date)} value={formik.values[formElement.name] ...

Creating Objects on the Fly with Mistic Query Builder

As someone who is relatively new to JavaScript, I have mainly focused on Java and PHP development. The JavaScript applications I have built in the past have been somewhat messy, difficult to test, and not easily extendable. Currently, I am working on crea ...

Tips for verifying elements using the Loop technique in a JSON array

I am new to JavaScript and I have been trying to run the following code with an expected result like this: [["00:04:12","05:54:46"],["06:06:42","12:45:22"],["12:51:11","15:56:11"]] However, my script is not working as expected. Can someone please help ...

Switch between two fields using just one animation

I've been experimenting with creating an information tag that slides out from behind a circular picture. To achieve this effect, I created a block and circle to serve as the information field placed behind the image. However, I'm facing a challe ...

Submitting a form from outside the form using a button in HTML: A step-by-step guide

I'm looking to submit a form using Angular on the functions next() and saveStep(). I'm unable to place next() and saveStep() within the form itself. I have set up my ng-click for next() and saveStep() below the form. When I submit the form fro ...

Sending a PDF document to a function that specifically calls for a file location or URL

Currently, I am developing a web application for an online library where I need to extract metadata from PDF files that are uploaded. To achieve this, I am utilizing the nodejs libraries pdf.js-extract and multer-gridfs-storage for file uploads. However, I ...

How can I display a "loading..." message as a temporary placeholder while waiting for my Apexcharts to load?

I've spent a day trying to solve this issue but couldn't find a solution. Any help would be greatly appreciated. Recently, I was working on creating a cryptocurrency tracker in React. I successfully built a table that displays multiple currencie ...

Tips for configuring jQtree to initially display the tree structure from the HTML source

Up to this point, I have utilized jQuery TreeView for the navigation menus on my website. However, as the main navigation menu has grown significantly in size (40869 bytes out of 67054 bytes), I am seeking a way to streamline it by using AJAX calls to fetc ...

Utilizing Vuex Store in Blade Template | Laravel and Vue.js

I'm trying to figure out how to access a method or variable that is defined in the Vuex 4 store within my Blade file. Currently, I am utilizing a compiled app.js from Vite. While I can easily access the store in Vue.js components, I am curious if it&a ...

Guide to uploading multiple images to an Amazon AWS S3 bucket using Node.js, Angular, and JavaScript

Can anyone share insights on where and how E-commerce websites typically upload their product images? Has anyone had experience using Amazon AWS S3 bucket for this purpose? Additionally, does anyone know how to upload or retrieve multiple images at once ...

Click a button to adjust the height of an element

My webpage features a dashboard with tabs on the left and corresponding content on the right. To ensure proper alignment, I have utilized Bootstrap to create two columns - one for the tabs and one for the content. However, I am facing an issue where the bo ...

creating an interactive table with the help of the useState hook

I'm new to JavaScipt and ReactJS, so I have a question that may seem obvious but I can't seem to figure it out. I am attempting to display my array in a table using useState, but currently I can only show the header. Additionally, if anyone know ...

"Utilize jQuery's append method to dynamically add elements to the DOM and ensure

I am currently facing an issue with my AJAX function that appends HTML to a page using the jQuery append method. The HTML being appended is quite large and contains cells with colors set by CSS within the same document. The problem I'm encountering i ...

Looking for a solution to dynamically fill a list in JQuery with data from a JSON file. Any suggestions for troubleshooting?

Currently, I am utilizing a JSON file to fetch Quiz questions. In my approach, each question is being stored in an array as an object. The structure of the question object includes 'text' (the actual question), 'choices' (an array of po ...