【问题标题】:In AngularJs When i call function not able to get $scope在 AngularJs 中,当我调用函数时无法获取 $scope
【发布时间】:2016-04-07 11:10:00
【问题描述】:

在 angularjs 中,当我单击按钮时,它会调用 Controller 的函数,但在该函数中我无法获取 $scope 或 $rootScope。

这里是链接: https://plnkr.co/edit/rFDGLPMdvW4BeTBdu5s8?p=preview

当您点击 Go To Store.. 商店页面将打开.. 并点击 Add to Cart.. 然后 $scope 函数将调用.. 但不会在该函数中获取 $scopeenter code here

【问题讨论】:

    标签: javascript jquery angularjs scope


    【解决方案1】:

    您有一个名为“MainController”的顶级控制器。 Store.html 视图使用此控制器,但 AddToCart 函数位于 StoreController 中。您需要将 Store 控制器添加到商店视图(store.html 的第一行):

    <div ng-controller="StoreController">
    

    Updated plunkr

    【讨论】:

    • 我已经在 AddToCart 函数中记录了 $scope(参见 plunkr),它工作正常
    【解决方案2】:

    您的应用运行良好,如果您打开开发者工具,您将看到debugger 断点被命中。我发现的问题是您没有为购物车定义路线。 $scope.cartItemCount 也不会自动更新

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-26
      • 1970-01-01
      • 2016-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多