AngularJS replace using regex on template

15,459

I modified your Plunker-Demo and it works pretty fine.

Hint: Don't use $scope namespaces like "document". Its reserved/used by the client.

var app = angular.module('plunker', []);

Controller

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';

  $scope.fileName = {
    content : function(){
      return '1233_test.txt'.replace(/\d+\_/,"");
    }
  }

  $scope.mpla = function () {
    console.log('clicked');
  }

});

View

<!DOCTYPE html>
<html ng-app="plunker">

<head>
  <meta charset="utf-8" />
  <title>AngularJS Plunker</title>
  <script>
    document.write('<base href="' + document.location + '" />');
  </script>
  <link rel="stylesheet" href="style.css" />
  <script data-require="[email protected]" src="https://code.angularjs.org/1.3.14/angular.js" data-semver="1.3.14"></script>
  <script src="app.js"></script>
</head>

<body ng-controller="MainCtrl">
  <p>Hello {{name}}!</p> 
  <a ng-click="mpla()" ng-bind="fileName.content()"></a>
</body>

</html>
Share:
15,459
Alex Arvanitidis
Author by

Alex Arvanitidis

Updated on June 26, 2022

Comments

  • Alex Arvanitidis
    Alex Arvanitidis almost 2 years

    I have the following markup that shows a value from ng-model.

    <a ng-click="downloadDocument()">{{document.content.replace(/\d+\_/,"")}}</a>
    

    Before each document.content I add a number and an underscore, smth like "12122141_document.txt". I want to replace this part by using this regex /\d+_/

    This throws an error on angularJS, although {{ document.replace(" ","") }} works.

    Is the only way to solve this a directive or am I doing something wrong?

    Plunker: http://embed.plnkr.co/sh54XZwSIlYnmvY0eTIt/preview

    Cheers,

    Alex