javascript - AngularJS 网页,持续加载

标签 javascript html css angularjs twitter-bootstrap

我正在使用 AngularJS Bootstrap Typeahead 插件。每当我尝试加载我的页面时,页面就会开始持续加载。这是我正在使用的代码:

HTML:

<!DOCTYPE html>
<html ng-app="myApp">
  <head>
    <script src="angular.js"></script>
    <script src="ui-bootstrap-tpls-0.11.0.min.js"></script>
    <script src="example.js"></script>
    <link href="bootstrap.min.css" rel="stylesheet">
  </head>
  <body>

    <script type="text/ng-template" id="customTemplate.html">
      <a>
          <img ng-src="{{match.model.favicon}}" width="16">
          <span bind-html-unsafe="match.label | typeaheadHighlight:query"></span>
      </a>
    </script>
    <div class='container-fluid' ng-controller="SearchBarCtrl">
        <input type="text" ng-model="customSelected" placeholder="Custom template" typeahead="searchItem as searchItem.name for searchItem in searchList | filter:{name:$viewValue}" typeahead-template-url="customTemplate.html" class="form-control">
    </div>
  </body>
</html>

JS:

angular.module('myApp', ['ui.bootstrap']);
function SearchBarCtrl($scope) {
  $scope.selected = undefined;
  var searchList = {  
    "list":{  
      "Channel":[  
        "Tech",
        "Gaming",
        "Design"
      ],
      "Category":[  
        "Tech",
        "Gaming",
        "Design"
      ]
    }
  };
  var channels = searchList.list.Channel;
  var categories = searchList.list.Category;
  $scope.searchList = [];
  for(var i = 0; i < (channels.length > categories.length) ? channels.length : categories.length; i++) {
    if(typeof channels[i] != 'undefined')
      $scope.searchList.push({'name':channels[i],'favicon':'img/techcrunch.ico'});
    if(typeof categories[i] !== 'undefined')
      $scope.searchList.push({'name':categories[i],'favicon':'img/techcrunch.ico'});
  }
}

发生了什么:

我已将所有脚本和 CSS 文件保存在同一目录中,并通过 XAMPP 服务器运行它们。每当我尝试打开我的网页时,页面不断加载,有时,在 Chrome 上,它显示 Waiting for plus.google.comWaiting for www.google.com.pk 和类似的无关系 URL,在状态栏上。

应该发生什么:

应该出现一个文本框,它应该实现 AngularJS Boostrap Typeahead 插件。差不多了,应该加载页面了。

最佳答案

问题出在for循环上。而不是你拥有的,而是使用这个:

 for (var i = 0; i < length; i++) {
        if (channels[i] !== undefined)
            $scope.searchList.push({ 'name': channels[i], 'favicon': 'img/techcrunch.ico' });
        if (categories[i] !== undefined)
            $scope.searchList.push({ 'name': categories[i], 'favicon': 'img/techcrunch.ico' });
 }

有问题的部分是 typeof channels[i] != 'undefined' 和 typeof categories[i] !== 'undefined' 行。我用功能相同的东西替换了它们。

关于javascript - AngularJS 网页,持续加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24788139/

相关文章:

html - IE8 并排 DIVS 正在堆叠

javascript - angularjs 指令不适用于动态内容

javascript - 设置最大高度不起作用

javascript - 使用 JSON 从 PHP 查询获取数据不起作用

javascript - 在任务栏浏览器选项卡上显示自定义文本

javascript - 要求YouTube API的视频在HTML页面中播放

javascript - 在 JavaScript 中,用于解析特定字符串的正确 `Temporal` 类型是什么?

javascript - Sprite 图像的宽度调整为 Ajax 生成的内容

css - SVG 中与比例无关的重复背景图像

html - 我可以在页面的每一侧都有一个表格行和单元格吗?