【问题标题】:pass AngularJS value / variable inside javascript在javascript中传递AngularJS值/变量
【发布时间】:2019-09-21 16:46:14
【问题描述】:

这是我的 Angular 数据(我在这里只写了一个数据条目作为演示,实际上我有很多条目):

var font = angular.module('font', []);

font.controller('fontListCtrl', function($scope) {

  $scope.font = [
                     {
                        id: "_001",
                        name: "Kalpurush",
                        download: "1"
                      }
                ]

    var download = scope.font.download
});

我想将我的下载 ID 传递到 html 中的 javascript 中。但我不能成功。

<div class="fontbox" ng-repeat="font in font">
{{font.name}}
<script>ccount_display('download')</script>
</div>

请帮帮我,谢谢:)

【问题讨论】:

  • 你到底想在这行做什么 -> &lt;script&gt;ccount_display('download')&lt;/script&gt;?你是想展示一些东西还是一个功能?如果是函数,请添加相关函数代码。
  • 为什么你的 div 中有一个 script 标签?

标签: javascript angularjs angularjs-ng-repeat


【解决方案1】:

我正在根据您的代码/问题做出一些假设。如果不是这种情况,我可以修改我的解决方案。 您的代码中有几个问题。我试图在下面修复它们:

var font = angular.module('font', []);
font.controller('fontListCtrl', function($scope) {
  $scope.fonts = [{
                    id: "_001",
                    name: "Kalpurush",
                    download: "1"
                  }];
     //assuming you're accessing above scope variable, your code had 'scope' but not $scope. And it's an array.
     $scope.download = $scope.fonts[0].download;
     //assuming you want to use download variable in your HTML.
});

HTML 也不正确。您应该执行以下操作:

ng-repeat="字体中的字体"

【讨论】:

  • 在您的 HTML 中,您正在执行 ng-repeat="font in font"... 两个变量的名称相同。所以我将其更改为 ng-repeat="font in fonts"在控制器中,范围变量现在是字体。
  • 好的,谢谢先生。我不专业。现在我怎样才能在&lt;script&gt;ccount_display(' in here ')&lt;/script&gt; 中引入这个范围变量?
  • 这里的意图是什么。您想在某些按钮单击时调用函数吗?
  • ccount_display 是一个下载计数器插件,我需要特定的 id 来显示特定项目的下载计数,例如 &lt;script&gt;ccount_display(' 1 ')&lt;/script&gt;
  • 所以对于 id 为 "_001" 的字体被下载 x 次。 id 为“_002”的字体已下载 y 次。这个值应该在字体名称旁边可见吗?
【解决方案2】:

如果我正确理解了问题,您希望将id 传递到count_display 方法中,您可以通过传递font.id 来做到这一点,并且在此上下文中使用&lt;script&gt; 标记是不必要的。希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-03
    • 2019-09-21
    • 2014-06-05
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多