2015-12-09 30 views
1

在ngDialog模板清晰格式的輸入值,我使用ngDialog [https://github.com/likeastore/ngDialog]創建模式,和我有正在當我點擊一個按鈕一個POST請求的基本形式。在同一按鈕上單擊,我想要清除表單輸入。通常,你可以做這樣的事情$scope.valueOfNgModel = "";但這不是在這種情況下工作。另外,ngDialog範圍,因爲它是實例化控制器的$範圍相同。不能在角

這裏的ngDialog實例:

$scope.addStudents = function() { 
    ngDialog.open({ 
    template: 'addStudents', 
    scope: $scope 
    }); 
}; 

這裏的ngDialog模板:

<script type="text/ng-template" id="addStudents"> 
    <h1> Add Students to Your Class </h1> 

    <form ng-model="myForm" ng-submit="addNewStudent(newStudent)"> 
    <input ng-model="newStudent.firstName" placeholder="First Name"> 
    <input ng-model="newStudent.lastName" placeholder="Last Name"> 
    <input ng-model="newStudent.email" placeholder="Email"> 
    <input ng-model="newStudent.image" placeholder="imageUrl"> 
    <h3> Note: Student Password by Default will be Firstname.lastname </h3> 

    <button class="ngdialog-button 
     ngdialog-button-primary" 
     type="submit">Add Student 
    </button> 
</form> 

</script> 

而下面是對FOMR運行提交功能(或點擊按鈕),我想清除所有輸入字段:

$scope.addNewStudent = function(student) { 

    var newUserToAdd = { 
    firstName: student.firstName, 
    lastName: student.lastName, 
    teacher: false, 
    email: student.email, 
    password: student.firstName + '.' + student.lastName, 
    classesBelongTo: [$scope.currentClassId], 
    image: student.image 
    }; 

    userService.postNewUser(newUserToAdd) 
    .then(function(response) { 
     $scope.loggedInUser = response.data; 
    }) 
    console.log("this is $scope.newStudent", $scope.newStudent); //this is undefined - I'm not sure why 

    $scope.newStudent = {}; //I tried this to clear it out 
    $scope.$apply(); //I tried this to no avail 
} 

任何幫助非常感謝!

回答

2

您可以在您的形式給出一個名字,然後調用$scope.yourFormName.$setPristine();

例:Fiddle