AngularJs 中的崩溃问题

Posted

技术标签:

【中文标题】AngularJs 中的崩溃问题【英文标题】:Issue with collapse in AngularJs 【发布时间】:2017-02-09 17:48:43 【问题描述】:

我知道这将是一个愚蠢的错误,但我想不通。

这是我的控制器

var app=angular.module('mainApp',['ngRoute','ui.bootstrap','ngAnimate']);

index.html

<head>
<meta charset="UTF-8">
<title>AngularJS | Basic LoginForm</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8//angular-route.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-animate.js"></script>
<script src="js/ui-bootstrap-tpls-2.1.4.min.js"></script>
<script src="controller.js"></script>
</head>
<body ng-app="mainApp">
    <div ng-view></div>
</body>

我得到的错误是

未捕获的错误:[$injector:modulerr]http://errors.angularjs.org/1.2.28/$injector/modulerr?p0=mainApp&p1=Error%3…gleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.28%2Fangular.min.js%3A18% 3A170)

它肯定与版本有关,因为即使我使用的是 1.5.8 版本,我也可以看到 angularJS 版本 1.2.28。

如果我删除 'ui.bootstrap','ngAnimate' 效果很好,但我需要它们来实现折叠功能

有人可以帮我解决这个问题吗?

【问题讨论】:

【参考方案1】:

您似乎缺少bootstrap.min.cssui-bootstrap-tpls-2.1.4.js 的路径问题

别忘了包含。

<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.1.4.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

看下面的例子来实现collapse功能。

Official Angular accordion

angular.module('ui.bootstrap.demo', ['ngAnimate', 'ngSanitize', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('AccordionDemoCtrl', function ($scope) 
  $scope.oneAtATime = true;

  $scope.groups = [
    
      title: 'Dynamic Group Header - 1',
      content: 'Dynamic Group Body - 1'
    ,
    
      title: 'Dynamic Group Header - 2',
      content: 'Dynamic Group Body - 2'
    
  ];

  $scope.items = ['Item 1', 'Item 2', 'Item 3'];

  $scope.addItem = function() 
    var newItemNo = $scope.items.length + 1;
    $scope.items.push('Item ' + newItemNo);
  ;

  $scope.status = 
    isCustomHeaderOpen: false,
    isFirstOpen: true,
    isFirstDisabled: false
  ;
);
<!doctype html>
<html ng-app="ui.bootstrap.demo">
  <head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-animate.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-sanitize.js"></script>
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.1.4.js"></script>
    <script src="example.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>

<div ng-controller="AccordionDemoCtrl">
  <script type="text/ng-template" id="group-template.html">
    <div class="panel panel-default">
      <div class="panel-heading">
        <h4 class="panel-title" style="color:#fa39c3">
          <a href tabindex="0" class="accordion-toggle" ng-click="toggleOpen()" uib-accordion-transclude="heading">
            <span uib-accordion-header ng-class="'text-muted': isDisabled">
              heading
            </span>
          </a>
        </h4>
      </div>
      <div class="panel-collapse collapse" uib-collapse="!isOpen">
        <div class="panel-body" style="text-align: right" ng-transclude></div>
      </div>
    </div>
  </script>

  <p>
    <button type="button" class="btn btn-default btn-sm" ng-click="status.open = !status.open">Toggle last panel</button>
    <button type="button" class="btn btn-default btn-sm" ng-click="status.isFirstDisabled = ! status.isFirstDisabled">Enable / Disable first panel</button>
  </p>

  <div class="checkbox">
    <label>
      <input type="checkbox" ng-model="oneAtATime">
      Open only one at a time
    </label>
  </div>
  <uib-accordion close-others="oneAtATime">
    <div uib-accordion-group class="panel-default" heading="Static Header, initially expanded" is-open="status.isFirstOpen" is-disabled="status.isFirstDisabled">
      This content is straight in the template.
    </div>
    <div uib-accordion-group class="panel-default" heading="group.title" ng-repeat="group in groups">
      group.content
    </div>
    <div uib-accordion-group class="panel-default" heading="Dynamic Body Content">
      <p>The body of the uib-accordion group grows to fit the contents</p>
      <button type="button" class="btn btn-default btn-sm" ng-click="addItem()">Add Item</button>
      <div ng-repeat="item in items">item</div>
    </div>
    <div uib-accordion-group class="panel-default" heading="Custom template" template-url="group-template.html">
      Hello
    </div>
    <div uib-accordion-group class="panel-default" is-open="status.isCustomHeaderOpen" template-url="group-template.html">
      <uib-accordion-heading>
        Custom template with custom header template <i class="pull-right glyphicon" ng-class="'glyphicon-chevron-down': status.isCustomHeaderOpen, 'glyphicon-chevron-right': !status.isCustomHeaderOpen"></i>
      </uib-accordion-heading>
      World
    </div>
    <div uib-accordion-group class="panel-danger" heading="Delete account">
      <p>Please, to delete your account, click the button below</p>
      <button class="btn btn-danger">Delete</button>
    </div>
    <div uib-accordion-group class="panel-default" is-open="status.open">
      <uib-accordion-heading>
        I can have markup, too! <i class="pull-right glyphicon" ng-class="'glyphicon-chevron-down': status.open, 'glyphicon-chevron-right': !status.open"></i>
      </uib-accordion-heading>
      This is just some content to illustrate fancy headings.
    </div>
  </uib-accordion>
</div>
  </body>
</html>

【讨论】:

谢谢。这是由于 ui-bootstrap-tpls-2.1.4.js 的路径问题【参考方案2】:

您似乎遇到了ui.bootstrap 的问题,这是您注入应用程序的索引 [1] 依赖项。您能否确保在您的浏览器检查器的“网络”选项卡中,ui.bootstrap 包含在您的 HTML 中?

第一步,尝试添加

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.1.4/ui-bootstrap-tpls.min.js"></script>

发给你的&lt;head&gt; 而不是

<script src="js/ui-bootstrap-tpls-2.1.4.min.js"></script>

这样我们就可以确定你的本地路径是否有问题。

专业提示:为了获得最佳性能,请将所有 Javascript 移动到 &lt;body&gt; 元素的底部,紧挨在结束 &lt;/body&gt; 标记之前。这将有助于更快的页面呈现。

【讨论】:

对不起。我是 AngularJS 的新手。你能告诉我我能做什么吗?

以上是关于AngularJs 中的崩溃问题的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 angularjs 操作导航栏引导程序 4 崩溃?

为啥这个 angularjs ui-router 代码会使我的浏览器崩溃?

AngularJS 使用控制器中的函数从服务中返回数据以用于 ng-repeat

AngularJS + Nginx CORS

AngularJS异常高的内存消耗

如何在 AngularJS 中使用 jQuery Select2