2016-03-04 113 views
3

我有3個文件:product-title.html,index.html調用product-title和app.js來創建我的指令。 我的瀏覽器沒有顯示在產品title.html代碼爲什麼我在AngularJs中的指令不起作用?

產品title.html

<h3> 
    {{product.name}} 
    <em class="pull-right">{{product.price | currency}}</em> 
</h3> 

的index.html

<html ng-app="gemStore"> 
<head> 
    <script type="text/javascript" src="angular.min.js"></script> 
    <script type="text/javascript" src="app.js"></script> 
</head> 
<body> 
<div class="list-group" ng-controller="StoreController as store"> 
    <div class="list-group-item cuerpo" ng-repeat="product in store.products"> 
    <product-title></product-title> 
    </div> 
</div> 
</body> 

app.js

(function() { 
var app = angular.module('gemStore', []); 

app.controller('StoreController', function(){ 
    this.products = gems; 
}); 

app.directive('productTitle', function(){ 
    return { 
    restrict: "E", 
    templateUrl: "product-title.html" 
    }; 
}); 
})(); 

寶石是一系列具有名稱,價格等的對象。 該代碼正在工作j直到我試圖創建第一個指令。

+1

您是否獲得在調試器的任何錯誤?任何網絡錯誤? – zero298

+1

在你寫的this.products = gems;這裏的寶石是什麼?你有什麼錯誤嗎? – Indra

+0

嘗試在控制器中導入$ scope並執行$ scope.products = gems; – mdegges

回答

0

可以幫助你。

<html ng-app="gemStore"> 
<head> 
    <script type="text/javascript" src="angular.min.js"></script> 
    <script type="text/javascript" src="app.js"></script> 
</head> 
<body> 
<div class="list-group" ng-controller="StoreController"> 
    <div class="list-group-item cuerpo" ng-repeat="product in products"> 
    <product-title></product-title> 
    </div> 
</div> 
</body> 

JS代碼:

var app = angular.module('gemStore', []); 
app.controller('StoreController', function($scope){ 
    var gems = [  
       {name: "LIVKR-2015", price: 20}, 
       {name: "LIVHCC-2015", price: 22}, 
       {name: "LIKKCC-2015", price: 24}, 
       {name: "LICPCC-2015", price: 20}, 
       {name: "LMLHCC-2015", price: 20} 
       ]; 
    $scope.products = gems;     
}); 
app.directive('productTitle', function(){ 
    return { 
    restrict: "E", 
    templateUrl: "product-title.html" 
    }; 
}); 
相關問題