2017-04-07 116 views
0

我是新來的angularjs,我設計了這個代碼來顯示食品,但我不知道問題是什麼......它沒有提供任何輸出。我嘗試了幾乎所有的東西,但我似乎無法找到問題所在。Angularjs代碼沒有顯示任何東西

<!DOCTYPE html> 
<html lang="en" ng-app="confusionApp"> 
<head> 
<meta charset="utf-8"> 
<meta http-equiv="X-UA-Compatible" content="IE=edge"> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<!-- The above 3 meta tags *must* come first in the head; any other head 
    content must come *after* these tags --> 
<title>Ristorante Con Fusion: Menu</title> 
    <!-- Bootstrap --> 
<link href="../bower_components/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet"> 
<link href="../bower_components/bootstrap/dist/css/bootstrap-theme.min.css" rel="stylesheet"> 
<link href="../bower_components/font-awesome/css/font-awesome.min.css" rel="stylesheet"> 
<link href="styles/bootstrap-social.css" rel="stylesheet"> 
<link href="styles/mystyles.css" rel="stylesheet"> 

<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> 
<!-- WARNING: Respond.js doesn't work if you view the page via file:// --> 
<!--[if lt IE 9]> 
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> 
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> 
<![endif]--> 
</head> 

<body> 

<div class="container"> 
    <div class="row row-content" ng-controller="dishDetailController"> 
     <div class="col-xs-12"> 
        <div class="media-left media-middle"> 
         <a href=""> 
          <img ng-src={{dish_cmnt.image}} alt="Uthapizza"> 
         </a> 
         <div class="media-body"> 
          <h2 class="media-heading">{{dish_cmnt.name}} 
         <span class="label label-danger">{{dish_cmnt.label}}</span> 
         <span class="badge">{{dish_cmnt.price | currency}}</span></h2> 
         <p>{{dish_cmnt.description}}</p> 
         </div> 
        </div> 
     </div> 
     <div class="col-xs-9 col-xs-offset-1"> 

     </div> 
    </div> 

</div> 

<script src="../bower_components/angular/angular.min.js"></script> 
<script> 

    var app = angular.module('confusionApp',[]); 

    app.controller('dishDetailController', function() { 

     var dish_cmnt={ 
         name:'Uthapizza', 
         image: 'images/uthapizza.png', 
         category: 'mains', 
         label:'Hot', 
         price:'4.99', 
         description:'A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.', 
         comments: [ 
          { 
           rating:5, 
           comment:"Imagine all the eatables, living in conFusion!", 
           author:"John Lemon", 
           date:"2012-10-16T17:57:28.556094Z" 
          }, 
          { 
           rating:4, 
           comment:"Sends anyone to heaven, I wish I could get my mother-in-law to eat it!", 
           author:"Paul McVites", 
           date:"2014-09-05T17:57:28.556094Z" 
          }, 
          { 
           rating:3, 
           comment:"Eat it, just eat it!", 
           author:"Michael Jaikishan", 
           date:"2015-02-13T17:57:28.556094Z" 
          }, 
          { 
           rating:4, 
           comment:"Ultimate, Reaching for the stars!", 
           author:"Ringo Starry", 
           date:"2013-12-02T17:57:28.556094Z" 
          }, 
          { 
           rating:2, 
           comment:"It's your birthday, we're gonna party!", 
           author:"25 Cent", 
           date:"2011-12-02T17:57:28.556094Z" 
          } 

         ] 
       }; 

     this.dish = dish; 

    }); 

</script> 

</body> 

</html> 
+2

我看不到'NG-應用=「confusionApp」'任何地方 –

+0

您的應用程序有多個問題,但突出的一個就是要定義你的'dish_cmnt'爲一個簡單的'var',它永遠不會被HTML訪問。它需要是'$ scope'的屬性,或者它需要是控制器的屬性。你可能想研究一些更有代表性的例子。 – Claies

+0

甚至當你添加'ngApp'時,它仍然不起作用,因爲你沒有使用ng-controller =「dishDetailController作爲vm」聲明你的控制器(然後 - '{{vm.dish_cmnt.name} }'等等。) –

回答

1

首先,你錯過了ng-app="confusionApp"所以我補充說。

接下來,您的模板沒有對dishDetailController的引用,所以使用ng-controller="dishDetailController as ctrl"並使用ctrl.dish_cmnt代替直接dish_cmnt

接下來,this.dish = dish出現錯誤,因爲dish未定義。無論如何,我們需要參考dish_cmnt,因此將其更改爲this.dish_cmnt = dish_cmnt

固定它。

這是您的工作代碼片段。

var app = angular.module('confusionApp', []); 
 

 
app.controller('dishDetailController', function() { 
 

 
    var dish_cmnt = { 
 
    name: 'Uthapizza', 
 
    image: 'images/uthapizza.png', 
 
    category: 'mains', 
 
    label: 'Hot', 
 
    price: '4.99', 
 
    description: 'A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.', 
 
    comments: [{ 
 
     rating: 5, 
 
     comment: "Imagine all the eatables, living in conFusion!", 
 
     author: "John Lemon", 
 
     date: "2012-10-16T17:57:28.556094Z" 
 
     }, 
 
     { 
 
     rating: 4, 
 
     comment: "Sends anyone to heaven, I wish I could get my mother-in-law to eat it!", 
 
     author: "Paul McVites", 
 
     date: "2014-09-05T17:57:28.556094Z" 
 
     }, 
 
     { 
 
     rating: 3, 
 
     comment: "Eat it, just eat it!", 
 
     author: "Michael Jaikishan", 
 
     date: "2015-02-13T17:57:28.556094Z" 
 
     }, 
 
     { 
 
     rating: 4, 
 
     comment: "Ultimate, Reaching for the stars!", 
 
     author: "Ringo Starry", 
 
     date: "2013-12-02T17:57:28.556094Z" 
 
     }, 
 
     { 
 
     rating: 2, 
 
     comment: "It's your birthday, we're gonna party!", 
 
     author: "25 Cent", 
 
     date: "2011-12-02T17:57:28.556094Z" 
 
     } 
 

 
    ] 
 
    }; 
 

 
    this.dish_cmnt = dish_cmnt; 
 

 
});
<head> 
 
    <meta charset="utf-8"> 
 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
 
    <!-- The above 3 meta tags *must* come first in the head; any other head 
 
    content must come *after* these tags --> 
 
    <title>Ristorante Con Fusion: Menu</title> 
 
    <!-- Bootstrap --> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
</head> 
 

 
<body ng-app="confusionApp"> 
 

 
    <div class="container"> 
 
    <div class="row row-content" ng-controller="dishDetailController as ctrl"> 
 
     <div class="col-xs-12"> 
 
     <div class="media-left media-middle"> 
 
      <a href=""> 
 
      <img ng-src={{ctrl.dish_cmnt.image}} alt="Uthapizza"> 
 
      </a> 
 
      <div class="media-body"> 
 
      <h2 class="media-heading">{{ctrl.dish_cmnt.name}} 
 
       <span class="label label-danger">{{ctrl.dish_cmnt.label}}</span> 
 
       <span class="badge">{{ctrl.dish_cmnt.price | currency}}</span></h2> 
 
      <p>{{ctrl.dish_cmnt.description}}</p> 
 
      </div> 
 
     </div> 
 
     </div> 
 
     <div class="col-xs-9 col-xs-offset-1"> 
 

 
     </div> 
 
    </div> 
 

 
    </div> 
 

 

 
</body> 
 

 
</html>

+0

我試過這個,但是它沒有顯示內容,但{{ctrl..dish_cmnt.name}}和其他所有像這樣的 –

+0

@GiteshSawariya在你的控制器中應該定義'ctrl'後你應該做些什麼。所以在這裏,如果你想顯示'ctrl.name',你必須在你的控制器中有'this.name =「gitesh」' – tanmay

+0

是的,我已經擁有了我的控制器中的所有值 –