Angular.JS自定义指令;不起作用
Angular.JS Custom Directive doesn't work
我有下一个问题。我正在做Angular.js的官方教程,并尝试从自己身上运行他们所有的例子。我不知道为什么这些指令在我的项目中不起作用,而在他们的网站上却起作用。"不起作用"意味着指令在这种情况下什么也没做。我希望看到product-galley.html中指定的图片库。这太令人沮丧了。现在你有了index.html和.js文件。angular.min.js、app.js、bootstrap.min.css文件与index.html.位于同一文件夹中
<!DOCTYPE html>
<html ng-app="gemStore">
<head>
<link rel="stylesheet" type="text/css" href="bootstrap.min.css" />
<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript" src="app.js"></script>
</head>
<body ng-controller="StoreController as store">
<!-- Products Container -->
<div class="list-group">
<!-- Product Container -->
<div class="list-group-item" ng-repeat="product in store.products">
<h3>{{product.name}} <em class="pull-right">{{product.price | currency}}</em></h3>
<!-- Image Gallery -->
<product-gallery></product-gallery>
<!-- Product Tabs -->
<product-tabs></product-tabs>
</div>
</div>
</body>
</html>
.js文件:
(function() {
var app = angular.module('gemStore', []);
app.directive('productGallery',function(){
return {
restrict: 'E',
templateUrl: 'product-gallery.html',
controller: function(){
this.current = 0;
this.setCurrent = function(imageNumber){
this.current = imageNumber || 0;
};
},
controllerAs: 'gallery'
};
});
app.controller('StoreController', function() {
this.products = gems;
});
app.controller("ReviewController", function(){
this.review = {};
this.addReview = function(product){
product.reviews.push(this.review);
this.review = {};
};
});
app.directive("productDescriptions", function() {
return {
restrict: 'E',
templateUrl: "product-description.html"
};
});
app.directive("productReviews", function() {
return {
restrict: 'E',
templateUrl: "product-reviews.html"
};
});
app.directive("productSpecs", function() {
return {
restrict:"A",
templateUrl: "product-specs.html"
};
});
app.directive("productTabs", function() {
return {
restrict: "E",
templateUrl: "product-tabs.html",
controller: function() {
this.tab = 1;
this.isSet = function(checkTab) {
return this.tab === checkTab;
};
this.setTab = function(activeTab) {
this.tab = activeTab;
};
},
controllerAs: "tab"
};
});
var gems = [
{
name: 'Azurite',
description: "Some gems have hidden qualities beyond their luster, beyond their shine... Azurite is one of those gems.",
shine: 8,
price: 110.50,
rarity: 7,
color: '#CCC',
faces: 14,
images: [
"1.png",
"images/gem-05.gif",
"images/gem-09.gif"
],
reviews: [{
stars: 5,
body: "I love this gem!",
author: "joe@example.org"
}, {
stars: 1,
body: "This gem sucks.",
author: "tim@example.org"
}]
}, {
name: 'Bloodstone',
description: "Origin of the Bloodstone is unknown, hence its low value. It has a very high shine and 12 sides, however.",
shine: 9,
price: 22.90,
rarity: 6,
color: '#EEE',
faces: 12,
images: [
"2.png",
"images/gem-07.gif",
"images/gem-04.gif"
],
reviews: [{
stars: 3,
body: "I think this gem was just OK, could honestly use more shine, IMO.",
author: "JimmyDean@example.org"
}, {
stars: 4,
body: "Any gem with 12 faces is for me!",
author: "gemsRock@example.org"
}]
}, {
name: 'Zircon',
description: "Zircon is our most coveted and sought after gem. You will pay much to be the proud owner of this gorgeous and high shine gem.",
shine: 70,
price: 1100,
rarity: 2,
color: '#000',
faces: 6,
images: [
"3.png",
"images/gem-07.gif",
"images/gem-07.gif"
],
reviews: [{
stars: 1,
body: "This gem is WAY too expensive for its rarity value.",
author: "turtleguyy@example.org"
}, {
stars: 1,
body: "BBW: High Shine != High Quality.",
author: "LouisW407@example.org"
}, {
stars: 1,
body: "Don't waste your rubles!",
author: "nat@example.org"
}]
}
];
})();
和产品库指令html页面,例如:
<div ng-show="product.images.length">
<div class="img-wrap">
<img ng-src="{{product.images[gallery.current]}}" />
</div>
<ul class="img-thumbnails clearfix">
<li class="small-image pull-left thumbnail" ng-repeat="image in product.images">
<img ng-src="{{image}}" />
</li>
</ul>
</div>
我将您的代码复制到Plunker中,并将这些行包含在index.html的<head>
部分中。您需要确保Jquery、Bootstrap和Angular在您的项目中正确加载。Jquery需要在Bootstrap之前,因为Bootstrap依赖它。一旦我添加了这些依赖项,代码中的AngularJS指令就起作用了。如果你在VisualStudio中工作,你可以通过NuGet包管理器将这些添加到你的项目中。
<script data-require="jquery@*" data-semver="2.1.3" src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
<link data-require="bootstrap-css@*" data-semver="3.3.1" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
<script data-require="angular.js@*" data-semver="1.4.0-beta.6" src="https://code.angularjs.org/1.4.0-beta.6/angular.js"></script>
发生此错误是因为您直接从浏览器打开html文档(指令html代码)。只需从Web服务器提供代码并在本地主机上访问即可。
转到控制台中项目的目录并键入:(您需要为此安装python 3)
python -m http.server [<portNo>]
然后在浏览器中访问您的网站,只需将localhost:8000
作为地址
相关文章:
- Angular指令在alertify setContent内容中不起作用
- AngularJS指令部分应用的函数don'不起作用
- Google Maps API OverlayView()在AngularJS指令中不起作用
- mouseover和mouseleave文本颜色更改在指令链接中不起作用
- 这段jquery代码在angular指令中不起作用
- Angular Filter在指令表达式中不起作用
- Angular UI Select2指令搜索功能不起作用
- 新的自定义角度指令不起作用
- 对于使用传递的数据计算的局部范围变量,角度绑定在自定义指令中不起作用
- 滚动指令在Angular JS中不起作用
- 角度指令双向绑定不起作用
- Angular.JS自定义指令;不起作用
- 指令中选择输入的双向绑定不起作用
- ng中使用的指令包括dond'不起作用
- scope.$apply 是未定义的,在 scrolly 指令中不起作用
- AngularJS,自定义指令不起作用@ plnkr
- 指令的角度优先级不起作用
- Angular js指令使用控制器作为语法ng点击不起作用
- 指令链接中的绑定不起作用
- ng 路由不起作用指令