2017-02-18 73 views
0

我目前堅持提交表單。我認爲我的ng-submit不起作用。我試圖在Chrome中運行沒有任何作品,而在Firefox中它運作。好像越來越糊塗 這裏是我的代碼:角js ng提交不工作

//控制器

app.controller("HomeController",["$scope","suggestions",function($scope,suggestions){ 
$scope.posts = suggestions.posts; 

$scope.addSuggestion = function(){ 
    //if input empty 
    if(!$scope.title || $scope.title === ""){ 
    alert("wrong"); 
    } 

    //push suggestions 
    $scope.posts.push({ 
    title: $scope.title, 
    upvotes: 0 

    }); 

    //after submit, clear input 
    $scope.title = ""; 
}; 
}]); 

// HTML

<body ng-app="SuggestionBox" ng-controller="HomeController"> 
<h1 class="text-center">Suggestion Box</h1> 
<section id="main"> 
    <div class="container"> 
    <div class="row"> 
     <div class="col-md-6 col-md-offset-3"> 
     <div class="well" ng-repeat="post in posts | orderBy:'-upvotes'"> 
      <h3>{{post.title}}</h3> 
      <button type="button" class="btn btn-warning"><i class="fa fa-star"></i> {{ post.upvotes }}</button> 
     </div> 
     </div> 
    </div> 

    <div class="row"> 
     <div class="col-md-6 col-md-offset-3"> 
     <form ng-submit="addSuggestion()"> 
      <h3 class="text-center">Submit Your Suggestion</h3> 
      <div class="form-group"> 
      <input type="text" class="form-control" placeholder="Great Ideas Here" ng-model="title"/> 
      </div> 
      <button type="submit" class="btn btn-primary">Suggest</button> 
     </form> 
     </div> 
    </div> 
    </div> 
</section> 
+0

你見過瀏覽器開發者控制檯嗎? – statosdotcom

回答

0
try this.. 

<button type="submit" class="btn btn-primary">Suggest</button> 
//change this line- 
<input type="submit" class="btn btn-primary" name="" id="" value="Suggest" /> 

hope it works. 
+0

嘿它工作!謝謝,但爲什麼它通過使用輸入代替按鈕? – GMassimiliano

0
<html lang="en"> 

<head> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script> 
<script> 
    angular.module('SuggestionBox', []).controller("HomeController", ["$scope", function($scope) { 
     $scope.posts = []; 

     $scope.addSuggestion = function() { 
      //if input empty 
      if (!$scope.title || $scope.title === "") { 
       alert("wrong"); 
      } 

      //push suggestions 
      $scope.posts.push({ 
       title: $scope.title, 
       upvotes: 0 

      }); 

      //after submit, clear input 
      $scope.title = ""; 
     }; 
    }]); 
</script> 
</head> 

<body ng-app="SuggestionBox" ng-controller="HomeController"> 
<h1 class="text-center">Suggestion Box</h1> 
<section id="main"> 
    <div class="container"> 
     <div class="row"> 
      <div class="col-md-6 col-md-offset-3"> 
       <div class="well" ng-repeat="post in posts | orderBy:'-upvotes'"> 
        <h3>{{post.title}}</h3> 
        <button type="button" class="btn btn-warning"><i class="fa fa-star"></i> {{ post.upvotes }}</button> 
       </div> 
      </div> 
     </div> 

     <div class="row"> 
      <div class="col-md-6 col-md-offset-3"> 
       <form ng-submit="addSuggestion()"> 
        <h3 class="text-center">Submit Your Suggestion</h3> 
        <div class="form-group"> 
         <input type="text" class="form-control" placeholder="Great Ideas Here" ng-model="title" /> 
        </div> 
        <button type="submit" class="btn btn-primary">Suggest</button> 
       </form> 
      </div> 
     </div> 
    </div> 
</section> 
</body> 

</html> 

此代碼工作按您的要求。建議

app.controller("HomeController",["$scope","suggestions",function($scope,suggestions){ 

的依賴建議是爲你創造的問題。

+0

無論如何「建議」是我的服務,所以我得到並存儲它 – GMassimiliano

+0

你可以提供寫在這個建議服務裏面的代碼嗎? –

+0

它已經解決了,謝謝你的幫助 – GMassimiliano