2013-10-25 127 views
-1

我是學習和它的第一天角js!雖然我已經學會了模型的控制器,視圖angular js是如何工作的,不顯示變量fowllowing代碼,而是給正常{{}} HTML看法,並沒有ng-repeat工作:Angular JS不能正常工作

<html ng-app='myApp'> 
    <head> 
     <title>Your Shopping Cart</title> 
    </head> 
    <body ng-controller='CartController'> 
     <h1>Your Order</h1> 
     <div ng-repeat='item in items'> 
      <span>{{item.title}}</span> 
      <input ng-model='item.quantity'> 
      <span>{{item.price | currency}}</span> 
      <span>{{item.price * item.quantity | currency}}</span> 
      <button ng-click="remove($index)">Remove</button> 
     </div> 
     <script src="lib/angular.js"></script> 
     <script> 
      function CartController($scope) { 
       $scope.items = [ 
        {title: 'Paint pots', quantity: 8, price: 3.95}, 
        {title: 'Polka dots', quantity: 17, price: 12.95}, 
        {title: 'Pebbles', quantity: 5, price: 6.95} 
       ]; 
       $scope.remove = function(index) { 
        $scope.items.splice(index, 1); 
       } 
      } 
     </script> 
    </body> 
</html> 

哪些錯誤的代碼?

回答

4

你應該定義你的myApp模塊:

var app = angular.module('myApp', []); 
app.controller('CartController', ['$scope', function($scope) { 
    $scope.items = [ 
     {title: 'Paint pots', quantity: 8, price: 3.95}, 
     {title: 'Polka dots', quantity: 17, price: 12.95}, 
     {title: 'Pebbles', quantity: 5, price: 6.95} 
    ]; 
    $scope.remove = function(index) { 
     $scope.items.splice(index, 1); 
    } 
}]); 

DEMO

+1

+1演示教授如何做正確的方式。 – gustavohenke

5

只是

<html ng-app> 

更換

<html ng-app='myApp'> 

,它應該工作。

用ng-app ='myApp'告訴angularjs你有一個名爲myApp的模塊。但是你沒有定義模塊。

1

基本上你沒有在控制器定義module

<script> 
angular.module('myApp', []); // add this line 

     function CartController($scope) { 
      $scope.items = [ 
       {title: 'Paint pots', quantity: 8, price: 3.95}, 
       {title: 'Polka dots', quantity: 17, price: 12.95}, 
       {title: 'Pebbles', quantity: 5, price: 6.95} 
      ]; 
      $scope.remove = function(index) { 
       $scope.items.splice(index, 1); 
      } 
     } 
    </script> 

你在Plunker

+0

「模塊」不是「模型」。 –

+0

@AndréDion哎呀,謝謝 –