修改ng-click angularjs中的数据

时间:2016-05-12 07:57:26

标签: angularjs

我在调用ng-click时需要编辑数据。以下是代码:

<tr class="read" ng-repeat="data in records>
  <td>
   {{data.requestId}}
 </td>
 <td>
   {{data.status}}
 </td>
   <a height="20" class="pointer" 
   ng-click="download(data.downloadPath)" target="_self"></a>
 </td>

e.g。 - 如果data.downloadPath的值为 /opt/dummy/file.txt ,我需要删除第一个正斜杠并将路径指定为: opt / dummy / file.txt < /强>

是否可以在angularjs中进行编辑,或者是否需要编辑控制器代码:

someSrv.someFunction()
        .success(function(response){

          $scope.records = response.data;
}

4 个答案:

答案 0 :(得分:2)

Angular表达式仍然是常规JavaScript,所以我想你可以选择

ng-click="download(data.downloadPath.slice(1))"

或者,如果第一个/是可选的,并不总是存在,则使用正则表达式:

ng-click="download(data.downloadPath.replace(/^\//, ''))"

答案 1 :(得分:1)

最简单的方法是使用substr方法

<a  ng-click="download(data.downloadPath.substr(1))" target="_self"></a>

这将完全删除"/"/dir/index.php将成为dir/index.php

答案 2 :(得分:0)

您可以使用ng-if指令检查该值是否等于/opt/dummy/file.txt

<tr class="read" ng-repeat="data in records>
  <td>
   {{data.requestId}}
 </td>
 <td>
   {{data.status}}
 </td>
   <div ng-if="data.downloadPath == '/opt/dummy/file.txt'">
     <a height="20" class="pointer" 
     ng-click="download(data.downloadPath.slice(1))" target="_self"></a>
   </div>
   <div ng-if="data.downloadPath != '/opt/dummy/file.txt'">
     <a height="20" class="pointer" 
     ng-click="download(data.downloadPath)" target="_self"></a>
   </div>
 </td>

答案 3 :(得分:0)

试试这个

$scope.download = function(dataPath){
    var string = "dataPath";
    if (string.charAt(0) == "/") string = string.substr(1);
    if (string.charAt(string.length - 1) == "/") string = string.substr(0,string.length - 1);
    $scope.path = string;

}