Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS replace using regex on template

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

like image 828
Alex Arvanitidis Avatar asked Mar 05 '15 09:03

Alex Arvanitidis


1 Answers

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>
like image 194
lin Avatar answered Sep 27 '22 20:09

lin