2015-11-11 52 views
-1

我用angularjs編寫了一個代碼,它使用一個指令從show-category.html文件中提取一個類別列表,並在索引頁面上顯示它們,但我做了一切,加載index.html時無法顯示類別。在templatejrl中使用angularjs中的指令

app.js文件

app.directive('showCategories', function() { 
    return { 
     restrict: 'E', 
     templateUrl: 'show-categories.html' 


    }; 
}); 

你可以看到這裏plunker完整的代碼裏面: http://plnkr.co/edit/FSsNAq?p=preview

回答

1

你置於控制器的中間你的指令定義權,把它外面它作品(除了其他一些不存在的功能,你必須有):

app.controller("BookCtrl", function($scope) { 

    $scope.categories = [{ 
     "id": 0, 
     "name": "Type" 
    }, { 
     "id": 1, 
     "name": "Date" 
    }, { 
     "id": 1, 
     "name": "Name" 
    } 

    ]; 
    ... 

}); 

app.directive('showCategories', function() { 
    return { 
     restrict: 'E', 
     templateUrl: 'show-categories.html' 


    }; 
}); 

Plunker

+0

我不知道這個錯誤是如何通過我的,謝謝你! 我打算只顯示必要的代碼,以便您瞭解問題而不會被其他功能弄糊塗,因爲我的應用程序比這更長.. –

+0

@SamJoni發生了:) –