2

Using AngularJS, I want to access scope variable from inside a <script type="text/ng-template".

<script type="text/ng-template" id="firstDialogId">
    <div class="ngdialog-message" align="center" id="download">
        <h4 ng-show=isFrench>Télécharger la cartographie</h4>
        <h4 ng-show=isEnglish>Download cartography</h4>
        <a href="../downloads/PDF/{{currentLanguage}}/{{currentCartography}}.pdf" download>
            <img  src="../style/images/pdf-icon.png" alt="Download PDF" width="30%" height="30%">
        </a>   
        <a href="../downloads/VSD/{{currentCartography}}.vsd" download>
            <img border="0" src="../style/images/vsd-icon.png" alt="Download VSD" width="30%" height="30%">
        </a>   
        <a href="../downloads/PNG/{{currentLanguage}}/{{currentCartography}}.png" download>
            <img border="0" src="../style/images/PNG-icon.png" alt="Download PNG" width="30%" height="30%">
        </a>   

         <div class="ngdialog-buttons">
             <button type="button" class="ngdialog-button ngdialog-button-primary" ng-click="closeThisDialog('button')">Close</button>
         </div>
    </div>
</script>

isFrench and isEnglish are 2 booleans from my controller.

Same for currentCartography and currentLanguage, they are strings from my controller.

I also tried with getter inside and outside of the controller, same result.

Ellone
  • 3,644
  • 12
  • 40
  • 72

3 Answers3

5

For those falling into the same issue :

Using ngDialog, we need to precise we want to use the scope. In my case, I added the dialog open functions in my controller, I needed to edit the one I'm using in order to add the scope: $scope, line as follows :

$scope.openPlainCustomWidth = function () {
    $rootScope.theme = 'ngdialog-theme-plain custom-width';
    ngDialog.open({
        template: 'firstDialogId',
        controller: 'InsideCtrl',
        className: 'ngdialog-theme-plain custom-width',
        scope: $scope, // this line wasn't here before
        closeByDocument: false
    });
};
Ellone
  • 3,644
  • 12
  • 40
  • 72
1

three things you can try

  1. use ng-href instead of href

  2. for ng-show remove double curly {{

  3. if above two doesnt work use $parent (only if you are not using controller-as)

harishr
  • 17,807
  • 9
  • 78
  • 125
  • My bad for the double curly, I just forgot. I corrected it and chenged href to ng-href, but it didnt change anything. How do I use $parent ? Do you have any link/example ? – Ellone Apr 08 '15 at 13:54
  • `$parent.isFrench`, isFrench is on parent scope, if does not work just try `
    {{$parent | json}}
    `, or basically open batrange and check the scopes parent child relationshio
    – harishr Apr 08 '15 at 13:56
  • `

    ...

    ` is not working. `

    ...

    ` is always true, regardless the value of isFrench Also, since i wasnt sure how to use it, I also tried `

    ...

    ` but same, always true.
    – Ellone Apr 08 '15 at 14:07
  • no no.. u r getting it wrong.. `ng-show="$parent.isFrench"` see the quotes around it... and the `
    ` is not value of ng-show.. it is used to see the data...
    – harishr Apr 08 '15 at 14:12
  • `

    ...

    ` with or without quotes is always false.
    – Ellone Apr 08 '15 at 14:17
  • then its upto you to debug it further using batrang – harishr Apr 08 '15 at 14:54
  • I found my problem, it was specific to ngDialog that I am using. I don't know batarang but thanks, it looks usefull. – Ellone Apr 08 '15 at 15:03
0

You are using ng-show incorrectly, it doesn't use {{}} expressions.

Try: ng-show="isFrench"

Beyond that it isn't clear what you are asking

charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • Yea I forgot to remove the double curly but this is still not working. Well, I'm asking how to access my controller variabes, since I can't here. – Ellone Apr 08 '15 at 13:55
  • what do you mean by `access controller variables`? Please show how you are using template and where in your code you have problems. Do you have an isolated scope in a directive using the template? – charlietfl Apr 08 '15 at 14:01
  • Well, I mean that my isFrench and isEngilsh are not working here. And they are variable from my controller. I just can't access them here. I'm displaying them, and the value is correct. And about the template I'm using ngDialog that I found online. Here is the git : https://github.com/likeastore/ngDialog – Ellone Apr 08 '15 at 14:26
  • because the `ngDialog` directive is using isolated scope. Are you passing anything to `ng-dialog-scope` ? Where are docs for directive? – charlietfl Apr 08 '15 at 14:31
  • I edited my last comment, to provide git instead of js file, everythinf we can found about this ngDialog is there : https://github.com/likeastore/ngDialog – Ellone Apr 08 '15 at 14:41
  • Not clear how you are using it. Suggest you create a demo that replicates your issue. Can pass either controller or scope to the `open()` method and I'm not sure how you have it configured – charlietfl Apr 08 '15 at 14:44
  • Thanks man, I found what I needed to change. In my controller I needed to add `scope: $scope` in the concerned dialog open function to be able to use the $scope. – Ellone Apr 08 '15 at 14:55