ng-view对我的应用程序不起作用。

12 浏览
0 Comments

ng-view对我的应用程序不起作用。

app.js //这是我包含了另一个文件的app.js文件,其中包含HeaderCtrlModule

var app = angular.module("BooKart",["ngRoute","HeaderCtrlModule"]);
app.config(function($routeProvider){
$routeProvider
.when("/books",{
 templateUrl: "views/book-list.html",
 controller: "BookListCtrl"
 })
 .when("/kart",{
 templateUrl: "views/kart-list.html"
 })
 .otherwise({
  redirectTo: "/books"
 })
 });

kart-list.html //这加载购物车视图

This is Kart view.

book-list.html// 这加载图书列表视图

 

    • {{book.name}}

       

      {{book.price}}

       

        • Rating: {{book.rating}}

       

        • Binding: {{book.binding}}

       

        • Publisher: {{book.publisher}}

       

        • Released: {{book.releaseDate}}

       

       

      {{book.details}}

 

 

index.html

<!doctype html>
  
  
  
  
  
      
      {{appDetails.tagline}}

 

 

 

  

HeaderCtrl.js

angular.module("HeaderCtrlModule",[])
.controller("HeaderCtrl",["$scope",function($scope)
{
$scope.appDetails = {};
$scope.appDetails.title = "BooKart";
$scope.appDetails.tagline = "We have 1 Million Books for you";
}])
.controller("BookListCtrl",["$scope","$http",function($scope,$http){
{
$scope.books = [
    {
        imgUrl: "adultery.jpeg",
        name: "Adultery",
        price: 205,
        rating: 4,
        binding: "Paperback",
        publisher: "Random House India",
        releaseDate: "12-08-2014",
        details: "Linda, in her thirties, begins to question the routine and 
predictability of her days. In everybodys eyes, she has a perfect life-happy 
marriage, children and a career. Yet what she feels is an eno... View More"
    },
    {
        imgUrl: "geronimo.jpeg",
        name: "Geronimo Stilton Spacemice#2 : You're Mine, Captain!",
        price: 168,
        rating: 5,
        binding: "Paperback",
        publisher: "Scholastic",
        releaseDate: "01-07-2014",
        details: "Geronimo Stilton meets outer space in this cosmically fun 
 spin-off series!Meet Geronimo StiltonixHe is a spacemouse - the Geronimo 
 Stilton of a parallel universe! He is captain of the spaceship Mou... View 
 More"
    },
    {
        imgUrl: "life-or-death.jpeg",
        name: "Life or Death",
        price: 339,
        rating: 4,
        binding: "Paperback",
        publisher: "Hachette India",
        releaseDate: "01-04-2014",
        details: "Why would a man escape from prison the day before he's due 
   to be released? Audie Palmer has spent a decade in prison for an armed 
  robbery in which four people died, including two of his gang. Five... View 
More"
    },
    {
        imgUrl: "playing.jpeg",
        name: "Playing It My Way : My Autobiography",
        price: 599,
        rating: 5,
        binding: "Hardcover",
        publisher: "Hodder & Stoughton",
        releaseDate: "01-08-2014",
        details: "I knew that if I agreed to write my story, I would have to 
be completely honest, as thats the way I have always played the game and 
that would mean talking about a number of things I have not addr... View 
More"
    },
    {
        imgUrl: "the-fault.jpeg",
        name: "The Fault in Our Stars",
        price: 227,
        rating: 4.5,
        binding: "Paperback",
        publisher: "Penguin Books Ltd",
        releaseDate: "25-01-2013",
        details: "Despite the tumor-shrinking medical miracle that has 
 bought her a few years, Hazel has never been anything but terminal, her 
 final chapter inscribed upon diagnosis. But when a gorgeous plot twist n... 
 View More"
    },
    {
        imgUrl: "wings-of-fire.jpeg",
        name: "Wings of Fire: An Autobiography",
        price: 124,
        rating: 5,
        binding: "Paperback",
        publisher: "Universities Press",
        releaseDate: "25-08-2000",
        details: "Wings of Fire traces the life and times of India's former 
 president A.P.J. Abdul Kalam. It gives a glimpse of his childhood as well 
 as his growth as India's Missile Man. Summary of the Book Wings... View 
 More"
    }
$scope.addToKart = function(book)
{
    console.log("add to kart: ", book);
}
}]);

我认为代码看起来没问题,我也包含了#!/而不是# /,不确定为什么不起作用,请有人检查一下告诉我哪里出了问题。我对angular很新,控制台中也没有错误,所以也没有帮助。先感谢您。请帮帮我。

admin 更改状态以发布 2023年5月24日
0
0 Comments

您的代码中有一些问题:

在您的BookListCtrl函数中移除多余的{

更正您的路由,将其更改为:href="#/kart"

请参见此工作中的Plunker

您包含的脚本似乎没有问题,只是建议您的模块名称为BooKart,而不是BookKart

0
0 Comments

如果您不使用本地服务器,Chrome不会在现有的html中加载其他html文件。但是,Firefox浏览器可以正常工作。

尝试使用Visual Studio。在Visual Studio中创建一个项目并使用它运行。

0