AngularJS 在模板上使用正则表达式替换

AngularJS replace using regex on template

我有以下标记显示来自 ng-model 的值。

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

在每个 document.content 之前我添加一个数字和一个下划线,类似于“12122141_document.txt”。我想用这个正则表达式 /\d+_/

来替换这部分

尽管 {{ document.replace(" ","") }} 有效。angularJS 会引发错误。

解决这个问题的唯一方法是指令还是我做错了什么?

笨蛋:http://embed.plnkr.co/sh54XZwSIlYnmvY0eTIt/preview

干杯,

亚历克斯

我修改了你的 Plunker-Demo,它工作得很好。

提示:不要使用 $scope 命名空间,例如 "document"。它的 reserved/used 由客户提供。

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

控制器

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

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

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

});

查看

<!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="angular.js@1.3.x" 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>