0

I need to edit data when I am calling ng-click. Below is the code:

<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. - If the value of data.downloadPath is /opt/dummy/file.txt, I need to remove the first forward slash and give the path as: opt/dummy/file.txt

Is it possible to edit here in angularjs or do I need to edit the controller code:

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

          $scope.records = response.data;
}

4 Answers 4

2

An Angular expression is still regular JavaScript, so I guess you can go for

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

Or if the first / is optional and not always there, with a regular expression:

ng-click="download(data.downloadPath.replace(/^\//, ''))"
Sign up to request clarification or add additional context in comments.

2 Comments

"An Angular expression is still regular JavaScript". No it's not. Also because of that the regexp example won't work and throw error.
The first option does what I need. Thank you !
1

Easiest thing would be to use substr method

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

This would remove "/" altogether. /dir/index.php would become dir/index.php

Comments

0

You can use the ng-if directive to check if the value is equal to /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>

Comments

0

Try this

$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;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.