0

나는 angularJS에 초보자입니다. CRUD 작업으로 학습을 시작했습니다. 나는 다음 항목을 delete 때 페이지가 reloadangularJs를 사용하는 페이지 새로 고침

내가 너무 $location$route를 통과해야하는 문제를 얻고있다. 그때로 쓰고

config

app.config(function ($locationProvider, $routeProvider) { 
    $locationProvider.html5Mode(true); 
    $locationProvider.hashPrefix = '!'; 

    $routeProvider.when('/', { 
     templateUrl: '/views/index.html', 
     controller: 'MainCtrl' 
    }); 
    $routeProvider.when('/this',{ 
     templateUrl: '/views/about.html', 
     controller: 'MainCtrl' 
    }); 
}); 

과 행동의 성공 : 그리고 다음과 같이 구현

$location.path('/this'); 

을하지만 난 http://localhost/someapp에서 http://this하지만 페이지로 다음 url 변경이 작업을 수행 할 때 새로 고침하지 않습니다. 이 경우 어떻게해야합니까? 제발 도와주세요.

Edit : 여기

은 내 deletion code : 모든 데이터가 $scope.samples에 저장됩니다

$http({ 
    method: 'GET', 
    url: 'test.php' 
}). 
success(function (data, status, headers, config) { 
    $scope.samples = data; 
}). 
error(function (data, status, headers, config) { 
    alert("Unable to load data."); 
}); 

반환 : 나는 PHP 파일에서 데이터를 얻고 초기화에

$scope.deletecode = function (id) { 
    name = ''; 
    if (confirm("are you sure to Delete the name")) { 
     $http({ 
      method: 'POST', 
      url: 'rohit.php', 
      data: { 
       "name": name, 
       "id": id, 
       "delete": "true" 
      }, 
     }). 
     success(function (data, status, headers, config) { 
      alert("data deleted successfully"); 
      $location.path('/this'); 
     }). 
     error(function (data, status, headers, config) { 
      alert("Unable to load data."); 
     }); 
    } else { 
     alert("Data deletion cancelled by user ... "); 
    } 
}; 

두 가지 user_idname

+0

실제로 페이지를 새로 고칠 지 확신하지 않습니다. 항목을 삭제하면 올바르게 완료되면 페이지를 새로 고칠 필요없이보기가 업데이트됩니다. – xbonez

+0

보기가 업데이트되지 않습니다.보기를 어떻게 업데이트 할 수 있습니까? 삭제 기능을 게시하고 있습니다. –

+0

내 코드가 업데이트되었습니다. –

답변

1

http 요청으로 서버에서 항목을 삭제하지만 클라이언트 측 코드에서 삭제하지 않으므로보기가 업데이트되지 않습니다. 서버에서 삭제 된 클라이언트 측 코드도 클라이언트 측 코드에서 삭제하려고합니다.

// all items in an array 
$scope.items = [item1, item2]; 
// delete an item 
$scope.deletecode = function (id) { 
    name = ''; 
    if (confirm("are you sure to Delete the name")) { 
     $http({ 
      method: 'POST', 
      url: 'rohit.php', 
      data: { 
       "name": name, 
       "id": id, 
       "delete": "true" 
      }, 
     }). 
     success(function (data, status, headers, config) { 
      alert("data deleted successfully"); 
      // at this point, the item has been deleted from your server 
      // remove it from $scope.items as well 
      // removes 1 item at index idx 
      $scope.items.splice(idx, 1); 
     }). 
     error(function (data, status, headers, config) { 
      alert("Unable to load data."); 
     }); 
    } else { 
     alert("Data deletion cancelled by user ... "); 
    } 
}; 
+0

안녕하세요, idx는 내 경우에는 이드입니까? –

+0

idx는 삭제할 어레이의 항목 색인입니다. 그것을 추적해야합니다. 따라서 첫 번째 항목을 삭제하는 경우 'idx = 0'. 마지막 요소를 지우면'idx = $ scope.items.length - 1' – xbonez

+0

PHP 파일에서 뷰에 데이터를 가져오고'$ scope.samples = data'.it에 저장하면 user_id와 이름과 삭제 후 나는'$ scope.samples.splice (user_id, 1)'을하고있다. 이게 뭐니? –