2016-07-09 12 views
1

角路線,我使用angularJS在PHP應用程序的路由提交表單傳遞AngularJS - 表單參數沒有通過使用PHP

我有以下情況:

我已經主頁命名dashboard.php

並且我在ng-View insertEmployee頁面中打開insertEmployee,其中包含幾個字段。和形式標記看起來像

<form action="" method="post"> 
    <!-- all fields --> 

<input type="submit"> 

</form> 

當點擊提交,整個頁面刷新和$ _ POST [「paraName」]顯示未定義

我不想整個頁面即可得到重新加載。我想用參數在ngView中打開頁面。

有8-10參數,其中之一是員工照片

回答

0

刪除action="" method="post"屬性,你不希望瀏覽器實際發送POST請求。相反,你訂閱onsubmit事件和處理它自己:

<form ng-submit="save()"> 
    <!-- all fields --> 
    <input ng-model="$ctrl.user.username" type="text"> 
    <input type="submit"> 
</form> 

在控制器,你將實現save方法和在那裏做任何需要做的事情,例如使AJAX POST請求,並重定向到另一條路線:

function save() { 
    // post xhr and redirect 
    $http.post('/api/user', this.user) 
    .then(response => response.data) 
    .then(user => $location.path(`/user/details/${user.id}`)) 

    // or better with service 
    users.save(this.user) 
    .then(user => $location.path(`/user/details/${user.id}`))  
} 

參考文獻:

+0

我也希望傳遞參數。有8-10個參數 –

+0

所以傳遞參數,你可以做到。 – dfsq

+0

如何?我也想用這種形式上傳圖片 –