【问题标题】:Access scope from ng-template来自 ng-template 的访问范围
【发布时间】:2015-04-08 13:35:33
【问题描述】:

使用 AngularJS,我想从 <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>

isFrenchisEnglish 是来自我的控制器的 2 个布尔值。

currentCartographycurrentLanguage 相同,它们是来自我的控制器的字符串。

我还尝试在控制器内外使用 getter,结果相同。

【问题讨论】:

    标签: javascript html angularjs ng-dialog


    【解决方案1】:

    对于那些遇到同样问题的人:

    使用 ngDialog,我们需要精确地确定我们想要使用的范围。 就我而言,我在控制器中添加了对话框打开功能,我需要编辑我正在使用的功能,以便添加scope: $scope, 行,如下所示:

    $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
        });
    };
    

    【讨论】:

      【解决方案2】:

      你可以尝试三件事

      1. 使用ng-href 而不是href

      2. 对于 ng-show 删除双卷曲 {{

      3. 如果以上两个不起作用,则使用 $parent(仅当您不使用控制器时)

      【讨论】:

      • 我对双卷发不好,我只是忘记了。我更正了它并将href改为ng-href,但它没有改变任何东西。如何使用 $parent ?您有任何链接/示例吗?
      • $parent.isFrench,isFrench 在父范围内,如果不起作用,请尝试 &lt;pre&gt;{{$parent | json}}&lt;/pre&gt;,或者基本上打开 batrange 并检查范围父子关系
      • &lt;h4 ng-show=$parent.isFrench&gt;...&lt;/h4&gt; 不工作。 &lt;h4 ng-show="&lt;pre&gt;{{$parent.isFrench | json}}&lt;/pre&gt;"&gt;...&lt;/h4&gt; 总是正确的,不管 isFrench 的值是多少另外,因为我不知道如何使用它,我也尝试了 &lt;h4 ng-show="&lt;pre&gt;{{$parent | json}}.isFrench&lt;/pre&gt;"&gt;...&lt;/h4&gt; 但同样,总是正确的。
      • no no.. 你弄错了.. ng-show="$parent.isFrench" 看到它周围的引号......而&lt;pre&gt; 不是 ng-show 的值.. 它用于查看数据...
      • &lt;h4 ng-show="$parent.isFrench"&gt;...&lt;/h4&gt; 带或不带引号总是错误的。
      【解决方案3】:

      您错误地使用了ng-show,它没有使用{{}} 表达式。

      试试:ng-show="isFrench"

      除了不清楚你在问什么

      【讨论】:

      • 是的,我忘了删除双卷曲,但这仍然不起作用。好吧,我在问如何访问我的控制器变量,因为我不能在这里。
      • access controller variables 是什么意思?请说明您如何使用模板以及您的代码中哪里有问题。使用模板的指令中是否有隔离范围?
      • 好吧,我的意思是我的 isFrench 和 isEngilsh 在这里不起作用。它们是我的控制器的变量。我只是无法在这里访问它们。我正在显示它们,并且值是正确的。关于我使用的模板,我在网上找到了 ngDialog。这是 git:github.com/likeastore/ngDialog
      • 因为ngDialog 指令使用了隔离作用域。您是否将任何内容传递给 ng-dialog-scope ?指令文档在哪里?
      • 我编辑了我的最后一条评论,提供 git 而不是 js 文件,我们可以找到关于这个 ngDialog 的所有信息:github.com/likeastore/ngDialog
      猜你喜欢
      • 1970-01-01
      • 2016-10-03
      • 2016-11-28
      • 1970-01-01
      • 2016-12-26
      • 2019-11-06
      • 2013-03-03
      • 1970-01-01
      • 2016-06-24
      相关资源
      最近更新 更多