从角度控制器到codeigniter控制器共享日期

时间:2017-05-28 14:25:03

标签: angularjs codeigniter

我可以在Codeigniter控制器中使用角度控制器的数据吗?我需要这个数组$ scope.prosek。我想在我的Codeigniter控制器中使用它的数据并将其提交给数据库。

 var MojApp = angular.module("MojApp", []);

        MojApp.controller("SastojakCtrl", function($scope) {
            $scope.hrana = js_array;
            $scope.sastojci = [];
            $scope.ukupno = {kalorije : 0.0,proteini:0.0,uh:0.0,masti:0.0,kolicina:0.0};
            $scope.prosek = {kalorije : 0.0,proteini:0.0,uh:0.0,masti:0.0};

1 个答案:

答案 0 :(得分:0)

这是你可以建立的一个例子。



var MojApp = angular.module("MojApp", []);

MojApp.controller("SastojakCtrl", ['$scope', '$http', function($scope, $http) {
  $scope.prosek = {
    kalorije: 0.0,
    proteini: 0.0,
    uh: 0.0,
    masti: 0.0
  };
  $http({
    method: 'POST',
    url: '<api_host>/test/create/',
    data: $scope.prosek,
  }).then(function(res) {
    // do something with response from CI
  }, function(err) {
    console.log(err);
  });
}]);
&#13;
<head>
</head>

<body ng-app="MojApp">
  <div ng-controller="SastojakCtrl"></div>

  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
</body>
&#13;
&#13;
&#13;

在CodeIgniter routes.php中注册一个端点。说,

$routes['test/create'] = 'nutrition/create';

在控制器类中,

class Nutrition extends CI_Controller {
  public function create() {
    $all = $this->input->post(); // this is data posted from the angular controller

  }

}