AJAX

$http

重點在於conroller的function呼叫時要加上$http

[post]

基本方法:

mainApp.controller('tableCtrl', function($scope,$http) {
    $scope.getData = function(){
        var url = 'your ajax url';
        console.log(url);
        $http.post(url).success(function(rp) {
          console.log(rp);
        });
    }
    $scope.getData();
});
</script>

如果要帶變數,目前找到以下這種做法

mainApp.controller('tableCtrl', function($scope,$http) {
  var parameter = {data:'hihi',test:'hi2'};
  var url = 'index.html';
  $http({
      method: 'POST',
      url: url,
      data: $.param(parameter),
      headers: {'Content-Type': 'application/x-www-form-urlencoded'}
  })
  .success(function(rp) {
      console.log(rp);
  })
  .error(function(data, status, headers, config) {
      alert("請聯絡工程人員");
  })
  ;
});

json to array

json encode問題

如果指定的array含有key,則json encode時就會變成物件 反之則是array

$ary = array('a','b','c');
$obj = array('a'=>'a1','b'=>'b1','c'=>'c1');

echo json_encode($ary); //["a","b","c"]
echo json_encode($obj); //{"a":"a1","b":"b1","c":"c1"}

[jQuery - $.map]

透過jquery將json的物件格式轉換成array 如此一來在table的調整上可以指定orderBy

mainApp.controller('tableCtrl', function($scope,$http) {
    $scope.getData = function(){
        var url = 'your ajax url';
        console.log(url);
        $http.post(url).success(function(rp) {
          console.log(rp);
          if(!angular.isArray(rp) && angular.isObject(rp)){
            rp = $.map(rp, function(el) { return el });
          }
          console.log(rp);
        });
    }
    $scope.getData();
});
</script>

results matching ""

    No results matching ""