0

I made a directive to display a script tag in my view:

angular.module 'app'
.directive 'kwankoScript', ->
  restrict: 'E'
  replace: true
  template: '<script type="text/javascript">
    window.ptag_params = {
    zone: "transaction",
    transactionId: "id",
    currency: "EUR",
    customerId: "' + scope.user.uuid + '",
    siteType: "d"
    };
    </script>'  

I call this directive from my view dashboard-validate.html like so:

<kwanko-script></kwanko-script>

I can access {{transaction.uuid}} from the view, but I cannot from my directive.

I would like to replace transactionId: "id" by `transactionId: "{{transaction.uuid}}" but it doesn't work.

any help would be greatly appreciated.

UPDATED :

this is my directive now:

 angular.module 'paycarApp'
.directive 'kwankoScript', ->

  restrict: 'E'
  replace: true
  scope: 
    transaction-id: "="
  template: '<script type="text/javascript">
    window.ptag_params = {
    zone: "transaction",
    transactionId: {{ transaction-id }},
    currency: "EUR",
    customerId: "' + scope.user.uuid + '",
    siteType: "d"
    };
    </script>'  

and my view:

 <kwanko-script transaction-id="{{transaction.uuid}}"></kwanko-script>

If I don't put the curly braces, the directive is not executed

and this is the output

 <script type="text/javascript" transaction-id="" class="ng-scope"> 
    window.ptag_params = { 
    zone: "transaction", 
    transactionId: {{ transaction-id }}, 
    currency: "EUR",
    customerId: "7514c32b-0aec-1b00-a52d-e676ff62e297",
    siteType: "d" };
</script>
9
  • It looks like that angular (/jqlite) cannot cope with script tags inside a template. It can however cope with it if you include jquery before angular Commented Apr 12, 2016 at 8:38
  • I've tried by replacing the script in the template by a simple <div> and that still doesn't work. can I maybe send you a direct message ? Commented Apr 12, 2016 at 8:45
  • See jsfiddle.net/ynagdh2g/1, with a div it works Commented Apr 12, 2016 at 8:46
  • indeed that works fine. If I try to pass {{user.uuid}} it doesn't work anymore. is there a reason for that ? Commented Apr 12, 2016 at 9:05
  • Yes I use on the scope userId: "=", which means it will allready evaluate the variable and therefore the {{}} aren't necessary anymore. Commented Apr 12, 2016 at 9:08

2 Answers 2

1

Use the scope variable for this in your directive, where you can define which properties you want to bind to your directive. Make your directive like this:

angular.module 'app'
    .directive 'kwankoScript', ->

    restrict: 'E'
    replace: true
    template: '<script type="text/javascript">
        window.ptag_params = {
        zone: "transaction",
        transactionId: {{ transactionId }},
        currency: "EUR",
        customerId: {{ userId }},
        siteType: "d"
        };
        </script>'
    scope: {
        transactionId: "=",
        userId: "="
    }

Then do in your view:

<kwanko-script user-id="user.uuid" transaction-id="transaction.uuid"></kwanko-script>
Sign up to request clarification or add additional context in comments.

3 Comments

this doesn't evaluate {{ transaction-id }} <script type="text/javascript" transaction-id="" class="ng-scope"> window.ptag_params = { zone: "transaction", transactionId: {{ transaction-id }}, currency: "EUR", customerId: "undefined", siteType: "d" }; </script>
Lose the -id and make it transactionId (camelcasing) in your template, like in my given code
It looks like that angular (/jqlite) cannot cope with script tags inside a template. It can however cope with it if you include jquery before angular
0

You will have to pass scope data from controller to the directive scope. Ideally the scope of controller and directive are not isolated and hence we just need to bind them In View > pass controller scope uuid to the directive as shown below

<kwanko-script tid = "transaction.uuid"></kwanko-script>

In JS code > We need to make the directive aware of how to make use of controller scope data

app.directive 'kwankoScript', ->
  restrict: 'E'
  replace: true
  scope: {
         tid : '='
  }
  template: '<script type="text/javascript">
    window.ptag_params = {
    zone: "transaction",
    transactionId: {{tid}},
    currency: "EUR",
    customerId: "' + scope.user.uuid + '",
    siteType: "d"
    };
    </script>'  

2 Comments

this doesn't work. I've tried that already. I see transactionId: {{tid}}
Did you add the code scope: { tid : '=' } inside the directive as shown above

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.