2013-12-24 179 views
1

我正在嘗試我的第一個角度js列表示例。具有顯示列表中的問題,也有在控制檯:(列表不顯示在角度綁定?

HTML沒有錯誤

<body ng-controller="mycontroller"> 
    <form ng-submit="addtask()">total nr of tasks: {{myvar.length}} 
     <br />remaining: {{remaining()}} 
     <input type="text" ng-model="newtask" /> 
     <ul ng-repeat="var in myvar"> 
      <li> 
       <input type="checkbox" ng-model="myvardone" /> <span class="done-{{myvardone}}">{{var.text}}</span> 

      </li> 
     </ul> 
     <button type="submit">add</button> 
    </form> 
</body> 

腳本:

function mycontroller($scope) { 
     $scope.myvar = [{ 
      text: 'bert', 
      done: false 
     }, { 
      text: 'ed', 
      done: true 
     }, { 
      text: 'pet', 
      done: false 
     }]; 

     $scope.addtask = function() { 
      $scope.myvar.push({ 
       text: $scope.newtask, 
       done: false 
      }); 
     } 

     $scope.remaining = function() { 
      var count = 0; 
      angular.forEach($scope.myvar, function (t) { 
       if (t.done) { 
        count++ 
       } else { 
        count += 0; 
       } 
      }); 
      return count; 
     } 
    } 

的jsfiddle:http://jsfiddle.net/dingen2010/vc2bC/3/

+0

http://jsfiddle.net/vc2bC/5/ –

+1

您必須添加'NG-應用= 「對myApp」'和'angular.module( '對myApp',[ ]);'引導角 –

+0

剩下的數字,即非strikentrhough任務不工作,雖然? – user603007

回答

0

幾件事情

一個。錯過了ng-app

二。你不應該重複ul,但li

<li ng-repeat="var in myvar"> 
    <input type="checkbox" ng-model="var.done" /> 
    <span class="done-{{var.done}}">{{var.text}}</span> 
</li> 

三。當你添加時,你需要輸入任務名稱。

四,你不需要剩餘的功能。使用過濾器

{{ (myvar | filter:{done:true}).length }} 

http://jsfiddle.net/vc2bC/10/

1

簡單的演示可以像

<html ng-app>xxxxx</html> 

不要忘記「ng-app」

+0

剩下的數字,即不是strikentrhough的任務不工作,但? – user603007

0

不知道該死的,覺醒打敗了我...:/但是,只需簡單地將ng-controller標籤移動到一個包含並在其上放置一個ng-app標籤html標記。在這裏:

enter code herehttp://jsfiddle.net/vc2bC/8/

+0

其餘的數字,即不是strikentrhough的任務不工作,但? – user603007