2015-07-12 229 views
0

我的angularJS推送選項不起作用。請問有人可以告訴我哪裏出了問題,並解決問題嗎?我現在纔開始學習這種語言一週。請原諒,如果錯誤/錯誤是愚蠢的。AngularJS推不工作

謝謝

<body ng-app="app" ng-controller ="Ctrl" > 

    <style> 
     .alert{ 

      color: crimson; 
      font-size: 19px; 

     } 
     .form{ 

      width: 72%; 
      margin-left: 12%; 
      border: 2px solid gold; 
     } 

    </style> 

    <script> 
       var app = angular.module("app", []); 
       app.controller("Ctrl", function ($scope) { 
        $scope.main = [{"ngmail": "[email protected]", "pass": "thor", "cpass": "thor"}]; 

        $scope.add = function ($scope) { 
         $scope.main.push($scope.sf); 

        }; 

       }); 
    </script> 




    <div>  
     <div>  
      <form name="regform" class="form-horizontal" role="form" style=" margin: auto;" > 
       <div class="form-group"> 
        <label class="control-label col-sm-2" for="email3">Email:</label> 
        <div class="col-sm-4"> 

         <input type="email" class="form-control" placeholder="Enter email" id="email3" name="email" ng-model="sf.ngmail" required/> 
         <span class ="alert" ng-show=" regform.email.$touched && regform.email.$error.required"> Required</span> 
         <span class ="alert" ng-show=" !regform.email.$error.required && (regform.email.$touched && regform.email.$invalid)"> Invalid Email</span> 
        </div> 
       </div> 
       <div class="form-group"> 
        <label class="control-label col-sm-2" for="pwd3">Password:</label> 
        <div class="col-sm-4">   
         <input type="password" class="form-control" id="pwd3" placeholder="Enter password" ng-model="sf.pass" name="password" required/> 
         <span class ="alert" ng-show=" regform.password.$touched && regform.password.$error.required"> Required</span> 

        </div> 
       </div> 

       <div class="form-group"> 
        <label class="control-label col-sm-2" for="pwd4">Confirm password:</label> 
        <div class="col-sm-4">   
         <input type="password" class="form-control" id="pwd3" placeholder="Enter password" ng-model="sf.cpass" name="cpassword" required/> 
         <span class ="alert" ng-show=" regform.cpassword.$touched && regform.cpassword.$error.required"> Required</span> 

        </div> 
       </div> 

       <div class="form-group">   
        <div class="col-sm-offset-2 col-sm-10"> 
         <div class="checkbox"> 
          <label><input type="checkbox"> Remember me</label> 
         </div> 
        </div> 
       </div> 
       <div class="form-group">   
        <div class="col-sm-offset-2 col-sm-10"> 
         <button type="button" ng-click="add()" class="btn btn-default">Submit</button> 
        </div> 
       </div> 
      </form> 
     </div> 
    </div>  
    <br> 

    <div> 

     <table border="1"> 
      <thead> 
       <tr> 
        <th>Email</th> 
        <th>password</th> 
       </tr> 
      </thead> 

      <tbody > 
       <tr ng-repeat="m in main"> 

        <td>{{m.ngmail}}</td> 
        <td>{{m.pass}}</td> 
       </tr> 
      </tbody> 
     </table> 


    </div> 





</body> 

回答

3

正如你在函數的參數,它與名稱$scope殺控制器&的$scope存在依賴關係創造了一個新的變量使用$scope。你不需要在你的函數中添加$scope,它將在你的函數中已經可用。

代碼

$scope.add = function() { 
    $scope.main.push($scope.sf); 
}; 
2

你並不需要通過$範圍的功能。

嘗試這樣

$scope.add = function() { 
    $scope.main.push($scope.sf); 
}; 
+0

i增加1分鐘打漿你 –

1

你試圖推方法,無需範圍傳遞給功能

$scope.add= function(){ // no scope here 
     $scope.main.push($scope.sf); 
};