Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax Error: Token ':' is an unexpected token when passing variable to directive

I have a directive called iframely and I it inside an ng-repeat like this:

<iframely url="iterator.url"></iframely>

This just treats the value as the string "iterator.url", not the actual .url value. To experiment, I just put in a URL directly:

<iframely url="https://soundcloud.com/braxe1/braxe-one-more-chance"></iframely>

Which gives me the Syntax Error: Token ':' is an unexpected token error. The closest I've gotten to passing this value to the directive is:

<iframely url="'{{iterator.url}}'"></iframely> // note double and single quotes

This resolves the URL parameter of iterator, but also passes it along with the ' ' single-quotes as part of the string.


EDIT: Also tried that without the single quotes.

<iframely url="{{iterator.url}}"></iframely>

And got Error: [$parse:syntax] Syntax Error: Token '{' invalid key at column 2 of the expression [{{iterator.url}}] starting at [{iterator.url}}]

What is the correct way to do this?


EDIT2: Here is the code for the directive:

angular.module( 'iframely', [])

.directive( 'iframely', [ '$http', '$sce', function ( $http, $sce ) {
    return {
        replace: true,
        restrict: "E",
        scope: {
            url: '='
        },
        template: '<div ng-bind-html="content"></div>',
        link: function ( scope, element, attrs ) {
            $http( {
                url: 'http://localhost:8061/iframely',
                method: 'GET',
                params: {
                    url: attrs.url
                }
            })
            .then( function ( result ) {
                scope.content = $sce.trustAsHtml( result.data.html )
            })
        }
    }
}])
like image 418
Noah Avatar asked Feb 16 '15 15:02

Noah


People also ask

How do I fix the Bash “unexpected token” error?

In other examples we will look at Bash scripts that when executed fail with the “unexpected token” error. To fix the error in a single command it’s usually enough to add or remove some incorrect characters that cause the syntax error in the command.

What is syntax error near unexpected token?

The Syntax Error Near Unexpected Token is an error that’s Usually withstood from the existence of all system files. These frequent Windows errors are usually less difficult to repair.

What is the difference between <> and expected token?

The data contains URL and read that data and bind to table. '>' is an unexpected token. The expected token is '"' or '''. Line 1, position 50. Any Body can Please share solution to me.

What does ‘then ‘ mean in a syntax error?

There is a syntax error. The token ‘ then ‘ is unexpected. All good! Lesson 3: When you see a syntax error verify that you are using Bash loops or conditional constructs in the right way and you are not adding any statements that shouldn’t be there.


2 Answers

You must replace url: '='

By url: '@'

https://docs.angularjs.org/api/ng/service/$compile

like image 168
Deblaton Jean-Philippe Avatar answered Oct 20 '22 23:10

Deblaton Jean-Philippe


Change your directive to be the following:

angular.module( 'iframely', [])

.directive( 'iframely', [ '$http', '$sce', function ( $http, $sce ) {
    return {
        replace: true,
        restrict: "E",
        scope: {
            url: '@'
        },
        template: '<div ng-bind-html="content"></div>',
        link: function ( scope, element, attrs ) {
            $http( {
                url: 'http://localhost:8061/iframely',
                method: 'GET',
                params: {
                    url: scope.url
                }
            })
            .then( function ( result ) {
                scope.content = $sce.trustAsHtml( result.data.html )
            })
        }
    }
}])

Notice the '@' in the scope and the url: scope.url.

like image 45
adam0101 Avatar answered Oct 20 '22 23:10

adam0101