Angular.JS自定义指令不起作用

时间:2015-03-23 20:41:33

标签: javascript html angularjs

我有下一个问题。我正在做官方的Angular.js教程并尝试从我自己的所有示例中运行。我不知道为什么这些指令在我的项目中不起作用,而在他们的网站上工作正常。 '不工作'意味着指令在这种情况下什么都不做。我希望在product-gallery.html中看到图片库的具体情况。 这令人沮丧。 Bellow你有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>

2 个答案:

答案 0 :(得分:0)

我将您的代码复制到Plunker中,并将这些行包含在index.html的<head>部分中。您需要确保在项目中正确加载Jquery,Bootstrap和Angular。由于Bootstrap依赖于它,Jquery需要在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>

答案 1 :(得分:0)

发生此错误是因为您直接从浏览器打开html文档(您的指令html代码)。只需从Web服务器提供代码并在localhost上访问它。

在控制台中转到项目目录 并键入:(您需要安装python 3)

python -m http.server [<portNo>]

然后在浏览器中访问您的网站,只需将localhost:8000作为地址