At a point in my html I'm doing things like this :
<li ng-repeat="favorite in favorites track by $index">
<a ng-href="javascript:void(0)" ng-click="changeSVG(favorite)">
<i class="fa fa-sitemap"></i>{{favorite}}
</a>
</li>
The problem is that sometimes the favorite
in ng-click="changeSVG(favorite)"
contains special characters like '
. So I'm getting errors like this in console :
Error: [$parse:lexerr] http://errors.angularjs.org/1.3.14/$parse/lexerr?p0=Unterminated%20quote&p1=s%2042-44%20%5B')%5D&p2=changeSVG('Process%20passageNaN'ordre%20MOB') at Error (native)
How can I prevent this ?
I heard about $sce when looking into it but not sure if it fits my needs and how to use it in my controller.
Here is the changeSVG()
function :
$scope.changeSVG = function (svgName) {
var defaultZoom = getZoomFromCarto(svgName);
$scope.currentCartography = svgName;
$scope.currentZoom = defaultZoom;
if ($scope.cartoHistory.indexOf(svgName) != -1)
$scope.cartoHistory.splice($scope.cartoHistory.indexOf(svgName), 1);
$scope.cartoHistory.unshift(svgName)
if ($scope.cartoHistory.length > 20)
$scope.cartoHistory = $scope.cartoHistory.slice(0, 20);
localStorage.setItem("cartoHistory", JSON.stringify($scope.cartoHistory));
removeEmbed();
var svgPath = "SVG/" + $scope.currentLanguage + "/" + svgName + ".svg";
lastEmbed = createNewEmbed(svgPath, defaultZoom);
}
I tried to display the svgPath in the log, it works fine with normal files, but when I try with my file with (space) and
'
in its name, nothing is displayed.
You can declare variables in html code by using a template element in Angular 2 or ng-template in Angular 4+. Templates have a context object whose properties can be assigned to variables using let binding syntax. Note that you must specify an outlet for the template, but it can be a reference to itself.
click(function(){ var fn = $("#folderName"). val(); var regex = /^[0-9a-zA-Z\_]+$/ alert(regex. test(fn)); }); }); This return false for special chars and spaces and return true for underscore, digits and alphabets.
The problem occurs as Angular replaces that favorite
at your function call by a string. A solution could be use $index
as the function parameter, so you can read from the array favorites
at your controller code adding any validation you may need. And no angular string replacement magic shall occur.
something like:
Template:
<li ng-repeat="favorite in favorites track by $index">
<a ng-href="javascript:void(0)" ng-click="changeSVG($index)">
<i class="fa fa-sitemap"></i>{{favorite}}
</a>
</li>
Controller:
$scope.changeSVG = function (index) {
// Add any validation logic here.
var svgName = favorites[index];
var defaultZoom = getZoomFromCarto(svgName);
$scope.currentCartography = svgName;
$scope.currentZoom = defaultZoom;
if ($scope.cartoHistory.indexOf(svgName) != -1)
$scope.cartoHistory.splice($scope.cartoHistory.indexOf(svgName), 1);
$scope.cartoHistory.unshift(svgName)
if ($scope.cartoHistory.length > 20)
$scope.cartoHistory = $scope.cartoHistory.slice(0, 20);
localStorage.setItem("cartoHistory", JSON.stringify($scope.cartoHistory));
removeEmbed();
var svgPath = "SVG/" + $scope.currentLanguage + "/" + svgName + ".svg";
lastEmbed = createNewEmbed(svgPath, defaultZoom);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With