2015-11-17 43 views
4

嘿,我是新的,今天已經是第二個問題,但我需要幫助 當我嘗試把ng-submit內的表單提交它的說屬性是不允許的並且不起作用 如果你可以幫我。 如果你可以向我解釋我什麼時候需要申報一個新的控制器? 和d完蛋了,非常感謝那些誰幫助 angularjs ng-submit不工作

(function() { 
 
    var app = angular.module('list', []); 
 

 
    app.controller('peopleListCtrl',['$scope', function($scope){ 
 
     $scope.persons = plists; 
 

 
     this.addPerson = function() { 
 
      if (this.text) { 
 
       persons.push(this.person); 
 
       this.text=''; 
 
      } 
 
     }; 
 
    }]); 
 

 

 
    var plists = [ 
 
     { name: 'alon', job: 'web dev', home:'nir tzvi' }, 
 
     { name: 'ben', job: 'katbamflighter', home:'nir tzvi' }, 
 
     { name: 'shiraz', job: 'dentist assistant', home:'hadera west' } 
 
    ]; 
 
})();
<!DOCTYPE html> 
 
<html ng-app="list"> 
 
<head> 
 
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js"></script> 
 
    <script type="text/javascript" src="app.js"> 
 
    </script> 
 
</head> 
 
<body ng-controller="peopleListCtrl"> 
 
<br /> 
 
<div ng-repeat="people in persons"> 
 
    <h3> 
 
    {{people.name}} 
 
    {{people.job}} 
 
    {{people.home}} 
 
    </h3> 
 
</div> 
 
<br /> 
 

 
<form name="personForm" ng-submit="peopleListCtrl.addPerson()" > 
 

 
    <input type="text" ng-model="person.name"/> 
 
    Name:{{person.name}} 
 
    <br /> 
 
    <input type="text" ng-model="person.job"/> 
 
    job:{{person.job}} 
 
    <br /> 
 
    <input type="text" ng-model="person.home"/> 
 
    home:{{person.home}} 
 
<br /> 
 
    <input type="submit" value="submit" /> 
 
</form> 
 

 
</body> 
 
</html>

回答

2

這裏是demo

(function() { 
 
    var app = angular.module('list', []); 
 

 
    app.controller('peopleListCtrl',['$scope', function($scope){ 
 
    var plists = [ 
 
     { name: 'alon', job: 'web dev', home:'nir tzvi' }, 
 
     { name: 'ben', job: 'katbamflighter', home:'nir tzvi' }, 
 
     { name: 'shiraz', job: 'dentist assistant', home:'hadera west' } 
 
    ]; 
 
     $scope.persons = plists; 
 
     $scope.persion = []; 
 
     $scope.addPerson = function() { 
 
      if ($scope.person) { 
 
       $scope.persons.push($scope.person); 
 
       $scope.person=null; 
 
      } 
 
     }; 
 
    }]); 
 
})();
<!DOCTYPE html> 
 
<html ng-app="list"> 
 
<head> 
 
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js"></script> 
 
    <script type="text/javascript" src="app.js"> 
 
    </script> 
 
</head> 
 
<body ng-controller="peopleListCtrl"> 
 
<br /> 
 
<div ng-repeat="people in persons"> 
 
    <h3> 
 
    {{people.name}} 
 
    {{people.job}} 
 
    {{people.home}} 
 
    </h3> 
 
</div> 
 
<br /> 
 

 
<form name="personForm" ng-submit="addPerson()" > 
 

 
    <input type="text" ng-model="person.name"/> 
 
    Name:{{person.name}} 
 
    <br /> 
 
    <input type="text" ng-model="person.job"/> 
 
    job:{{person.job}} 
 
    <br /> 
 
    <input type="text" ng-model="person.home"/> 
 
    home:{{person.home}} 
 
<br /> 
 
    <input type="submit" value="submit" /> 
 
</form> 
 

 
</body> 
 
</html>

+0

didnt解決仍然沒有任何反應就提出,當鼠標移到NG-提交它說屬性不允許在這裏 –

+0

工作! = D,但爲什麼我需要定義是否$ scope.person?我希望它只在有文本 –

+0

時才提交,因爲您在不定義'$ scope.person'的情況下使用'person.name'作爲'ng-model'。你需要首先定義'$ scope.person' .. –