[英]Angular.JS Custom Directive doesn't work
我有下一個問題。 我正在做官方的Angular.js教程,並嘗試從自己身上運行所有示例。 我不知道為什么這些指令在我的項目中不起作用,而在他們的網站上卻起作用。 “不起作用”表示指令在這種情況下不執行任何操作。 我希望看到在product-gallery.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"
}]
}
];
})();
以及product-gallery指令的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指令就會起作用。 如果您在Visual Studio中工作,則可以通過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服務器提供您的代碼並在localhost上訪問它即可。
在控制台中轉到項目目錄,然后輸入:(您需要為此安裝python 3)
python -m http.server [<portNo>]
然后在瀏覽器中訪問您的網站,只需輸入localhost:8000
作為地址
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.