Tuesday, 15 February 2011

angularjs - How to pass special characters in a function -



angularjs - How to pass special characters in a function -

i new angular, working on poc of how utilize modal in application. here doing:

i have controller declares function called open in scope. function open takes url input. now, url need pass in dynamically created:

<a ng-click="open(/test/obj['test-attr']/update/)" href="/test/{{ obj['test-attr'] }}/update/">{{ obj['test-attr'] }}</a>

but gives me parse error. not sure how pass info controller.

update:

the error not because of quotations because of /. there angular way pass in special character function.

this error get:

error: [$parse:lexerr] http://errors.angularjs.org/1.2.22/$parse/lexerr?p0=unexpected%20nextharacter%20&p1=s%205-5%20%5b%5c%5d&p2=open(%5c

your problem not going solved passing special characters. misguided.

the look in ng-click has syntax errors. remember "javascript code" (or "javascript expression" more precise). mean, type next in javascript?

open(/test/obj['test-attr']/update/)

...this won't fly in javascript. /test/ , /update/ string literals , should quoted. , concatenate strings, utilize old + (plus) sign. sum up, should like:

open('/test/' + obj['test-attr'] + '/update/')

here's syntax (and working plunker):

<a ng-click="open('/test/' + obj['test-attr'] + '/update/')" href="/test/{{ obj['test-attr'] }}/update/">{{ obj['test-attr'] }} </a>

http://plnkr.co/edit/uctqc5byrr0wmvyui5nx

potential second issue: next off-topic related code in plunker. not reply original question, issue ran while testing plunker example.

window.open() in angular expressions not straight available.

these expressions not "javascript", rather "angularjs expressions".

it appears code attempting phone call "open()" (a global javascript object - not accessible in "angularjs expressions"). angular keeps tight command on what's accessible globally.

i mean, can access stuff in $scope within these expressions. created "open" method in $scope calls $window.open(). ($window angularjs wrapper "window" unit testing/mock, angular way of calling global javascript functions).

var app = angular.module('plunker', []); app.controller('mainctrl', function($scope, $window) { $scope.obj = { "test-attr": "testattrdata" }; $scope.open = function() { $window.open.apply(null, arguments); } });

i suspect might have third issue: open() called not prevent href action take normal effect of reloading page. if that's case add together $event.preventdefault(); in ng-click expression:

ng-click="open('/test/' + obj['test-attr'] + '/update/'); $event.preventdefault();"

angularjs escaping angularjs-controller

No comments:

Post a Comment