javascript - 复制 UI-Bootstrap 代码不起作用?如何使用 UI-Bootstrap?

标签 javascript html angularjs angular-ui-bootstrap carousel

我正在尝试使用 http://angular-ui.github.io/bootstrap/ 中的组件页面,我基本上完全复制代码只是为了获取框架。 但它似乎不起作用,我是否错过了一些一般性的东西?

这是完全相同的代码,据我所知,我添加了依赖项,这是其他类似帖子中的错误。

例如,对于轮播(ui.bootstrap.carousel),我将可用的 html 代码复制到 html 文件 index.html 中:

<!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.3.0.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="CarouselDemoCtrl">
<div style="height: 305px">
<div uib-carousel active="active" interval="myInterval" no-wrap="noWrapSlides">
  <div uib-slide ng-repeat="slide in slides track by slide.id" index="slide.id">
    <img ng-src="{{slide.image}}" style="margin:auto;">
    <div class="carousel-caption">
      <h4>Slide {{slide.id}}</h4>
      <p>{{slide.text}}</p>
    </div>
  </div>
</div>
</div>
<div class="row">
<div class="col-md-6">
  <button type="button" class="btn btn-info" ng-click="addSlide()">Add Slide</button>
  <button type="button" class="btn btn-info" ng-click="randomize()">Randomize slides</button>
  <div class="checkbox">
    <label>
      <input type="checkbox" ng-model="noWrapSlides">
      Disable Slide Looping
    </label>
  </div>
</div>
<div class="col-md-6">
  Interval, in milliseconds: <input type="number" class="form-control" ng-model="myInterval">
  <br />Enter a negative number or 0 to stop the interval.
</div>
</div>
</div>
</body>
</html>

我已将 js 代码复制到名为 example.js 的 js 文件中:

angular.module('ui.bootstrap.demo', ['ngAnimate', 'ngSanitize', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('CarouselDemoCtrl', function ($scope) {
$scope.myInterval = 5000;
$scope.noWrapSlides = false;
$scope.active = 0;
var slides = $scope.slides = [];
var currIndex = 0;

$scope.addSlide = function() {
var newWidth = 600 + slides.length + 1;
slides.push({
  image: '//unsplash.it/' + newWidth + '/300',
  text: ['Nice image','Awesome photograph','That is so cool','I love that'][slides.length % 4],
  id: currIndex++
});
};

$scope.randomize = function() {
var indexes = generateIndexesArray();
assignNewIndexesToSlides(indexes);
};

for (var i = 0; i < 4; i++) {
$scope.addSlide();
}

// Randomize logic below

function assignNewIndexesToSlides(indexes) {
for (var i = 0, l = slides.length; i < l; i++) {
  slides[i].id = indexes.pop();
}
}

function generateIndexesArray() {
var indexes = [];
for (var i = 0; i < currIndex; ++i) {
  indexes[i] = i;
}
return shuffle(indexes);
}

// http://stackoverflow.com/questions/962802#962890
function shuffle(array) {
var tmp, current, top = array.length;

if (top) {
  while (--top) {
    current = Math.floor(Math.random() * (top + 1));
    tmp = array[current];
    array[current] = array[top];
    array[top] = tmp;
  }
 }

return array;
}
});

为了更好的可读性,您还可以在此处找到代码:https://plnkr.co/edit/YZULMBb0br4IuhV4dUug?p=preview

当我运行它时,它只在页面上显示:

幻灯片 {{slide.id}}

{{slide.text}}

为什么?

最佳答案

尝试在链接前添加 http:,plunker 会直接加载这些引用,但是当您直接在浏览器中加载网址时,它将无法工作。

有时不一定,因为如果用户不直接指定,几乎所有浏览器都会将其视为默认协议(protocol)

您需要添加http:

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js"></script> 
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-animate.js"></script> 
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-sanitize.js"></script> 
<script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.3.0.js"></script> 
<script src="example.js"></script> 
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

关于javascript - 复制 UI-Bootstrap 代码不起作用?如何使用 UI-Bootstrap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40962339/

相关文章:

javascript - 如何交换下拉值?

javascript - 当我使用 Route 时,为什么我的 props 未定义?

javascript - 我怎样才能使这个 AngularJS 应用程序搜索引擎友好?

css - 从轮播框架中获取轮播控制箭头

angularjs 向 ng-keydown 添加逻辑

html - -webkit-转换 :rotate() produces an inexplicable square unrelated to the element the CSS is applied to

javascript - 使用 JS 请求 - 这已经过时了吗?

javascript - 使用 JS 和 PHP 限制复选框选择

html - 应该在哪里设置网站的语言?

jquery - 如何使用 css 将文本或图像居中放置在固定大小的 div 上