0

My following code snippet is not working and I am continuously getting error. I am dynamically generating this content. But When it renders, I get errors.

I have tried escape(), but still it's not working.

$scope.teamInvites[index].teamInviteId has a value like 12334

tname has a value like Test Team 1

email has a value like [email protected]

 + "<a ng-click='deleteRow(" + rowID + "," + $scope.teamInvites[index].teamInviteId+")'>Delete</a> | "
                                  + "<a ng-click='reInvite(" + rowID + "," + tname + ","+ email+")'>ReInvite</a>" 

Error:

Error: [$parse:syntax] Syntax Error

Whenever there is @ or space in a variable, I get this error. don't know what's the work around here. Otherwise it's working fine.

2 Answers 2

1

You need to escape the strings in the generated expression. So (just the 2nd line):

+ "<a ng-click='reInvite(" + rowID + ","\" + tname + "\",\"" + email + "\")'>ReInvite</a>"
//                                      ^             ^  ^              ^
//                                      |             |  |              |
//  look here --------------------------+-------------+--+--------------+
Sign up to request clarification or add additional context in comments.

1 Comment

thank you, it's simple and it really solved my problem
0

You could use an array to build your string with rather than escaping characters - with Array.prototype.join.

It's rather performant and used widely wihtin the Angular community - for string defined templates:

    var a = [
        "<a ng-click='deleteRow(", rowId, ",", $scope.teamInvites[0], ")", "'>Delete</a> | ",
        "<a ng-click='reInvite(", rowID, ",", tname, ",", email ,")'>ReInvite</a>"
    ].join(''); // Join to a string.

1 Comment

You are missing the point: The problem is not performance - the problem is that the expression inside the ng-click attribute is invalid. The reason it is invalid is that a string is not quoted. This solution suffers from the same bug (but perhaps it will crash a few microseconds faster).

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.