【问题标题】:AngularJS: directive adds object to $scope but undefined in controllerAngularJS:指令将对象添加到 $scope 但在控制器中未定义
【发布时间】:2017-10-30 09:04:33
【问题描述】:

我是 AngularJS 的新手。 我想使用angular-bootstrap-tour 创建一个演练。它说:

tour 指令创建一个包含所有游览步骤的包装器,并将游览对象添加到范围。

所以我认为有这段代码:

<div tour placement="top">
   <div tour-step order="0" title="Main Menu" content="This is the main menu." placement="bottom">
</div>

现在在我的控制器中,我想使用此代码在页面加载时开始游览:

$scope.tour.restart(tour);

但它说:$scope.tour is undefined! 当我将下面的代码添加到我的视图中时,按钮开始游览!如何在视图中定义游览但在控制器的$scope 中没有定义?

<button class="btn" ng-click="tour.restart(true)">Start Tour</button> 

【问题讨论】:

  • 你在哪里执行$scope.tour.restart(tour);?根据您使用的 AngularJS 版本,您可能希望在 ngOnInit 挂钩中执行此操作。但是您可能需要添加更多代码来提供更多上下文。

标签: javascript angularjs twitter-bootstrap


【解决方案1】:

在你的js文件中添加代码

    function execute(moduleType) {
        if(moduleType) {
           var tour = new Tour({orphan: false});
           tour.addSteps(setRoute(moduleType));
           tour.init();
           tour.restart();
           tour.start(true);
        }
    }

在您的 HTML 文件中添加代码,例如

<button class="btn" ng-click="execute(true)">Start Tour</button> 

【讨论】:

  • Afaik,OP 表示使用没有问题:&lt;button class="btn" ng-click="tour.restart(true)"&gt;Start Tour&lt;/button&gt;
  • 感谢帮助我了解如何使用 Tour class 并可能解决我的问题;但我的问题仍未得到解答。
猜你喜欢
  • 2017-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多