5

Tôi đang tạo một trang web với Codeigniter 3 và AngularJs. Trong khi delevolping tôi đã phải đối mặt với một số vấn đề với url cơ sở trong codeigniter. Im đính kèm một tập tin hình ảnh của struture thư mục của tôi.AngularJS Codeigniter base_url cài đặt đường dẫn

enter image description here

Trong mainpage tôi (views/home.php) i đặt một ng-view cho tải các trang khác nhau. Ở đây tôi đang sử dụng angularjs ngRoute để định tuyến.

Đây là mã của tôi.

var myApp = angular.module("myApp",["ui.bootstrap","ui.router","ngRoute"]); 
myApp.config(function($routeProvider) { 
    $routeProvider 
    .when("/", { 
    templateUrl : "application/pages/mainpage.php" 
    }) 
    .when("/about", { 
    templateUrl : "application/pages/about.html" 
    }) 
    .when("/services", { 
    templateUrl : "application/pages/services.html" 
    }) 
    .otherwise("/",{ 
    templateUrl: "application/pages/mainpage.php" 
    }); 
}); 

Bên trong đó mainpage.php tôi liệt kê một số nội dung tiểu sử (nội dung giả) nằm bên trong tệp json (datas/data.json). Và tất cả nội dung đều được liệt kê. Và bên trong danh sách này tôi đặt một nút với ng-click = "Myname"

Dưới đây là đoạn code:

<div class="container" ng-controller="ListController"> 

<div class="row"> 
    <div class="col-lg-12 text-center"> 
     <h1>Starter Template</h1> 
     <p class="lead">Complete with pre-defined file paths that you won't have to change</p> 

     <div class="row"> 
      <div class="col-sm-6 col-md-4 wow zoomIn" data-wow-delay="0.5s" ng-repeat="items in artists"> 
       <div class="thumbnail"> 
        <img ng-src="{{settings.base_url}}/application/assets/images/profile/{{items.shortname}}.jpg" alt="..."> 

        <div class="caption"> 
         <h3>{{items.name}}</h3> 
         <p>{{items.bio}}</p> 
         <p><a href="javascrip:void(0);" class="btn btn-primary" ng-click="myName()" role="button">Button</a> <a href="#" class="btn btn-default" role="button">Button</a></p> 
        </div> 
       </div> 
      </div> 


     </div> 
    </div> 
    </div> 

    <!-- /.row --> 
</div> 

Tất cả các mã js của tôi đã wriiten trong app.js (tài sản/js/app.js) bao gồm Js góc. Trong tập tin js này tôi đã thiết lập một biến base_url giống như trong file code.php config.php

$ config ['base_url'] = 'http://localhost/AngularExPack/Angular_Bootstrap_CI_1/';

bên app.js

var BASE_URL = 'http://localhost/AngularExPack/Angular_Bootstrap_CI_1/'

Khi tôi bấm vào nút của tôi với ng-click = "Myname" tôi muốn có được một bộ điều khiển (controller CodeIgniter) và muốn thực hiện một hàm bên trong bộ điều khiển đó. Ở đây, tôi sử dụng auth.php (controllers/auth.php) và tên hàm là functionOne();

public function functionOne() 
    { 
     /*$this->load->view('home1');*/ 
     echo "hai"; 
    } 

Đối với iam đó bằng $ hhtp.post và var base_url cho đường dẫn. Nhưng tôi đã không nhận được đầu ra đó và nó không phải là đi qua để điều khiển.

 myApp.controller('ListController', ['$scope', '$http', 'settings', function($scope, $http) { 

       $http.get('application/datas/data.json').success(function(data) { 
        $scope.artists = data; 
       }); 

       $scope.myName = function() { 
        alert("hai1"); 
        /**/ 
        $http.post(base_url + 'auth/functionOne').success(function(response) { 
         $scope.data = response 
         console.log($scope.data); 
         alert($scope.data); 
        }); 

       } 

      }]); 

ng-click = "myName" hoạt động tốt. Nhưng nó không phải là đi qua chức năng functionOne bên trong đó auth.php điều khiển. Tôi nghĩ rằng vấn đề có lẽ là với đường dẫn base_url. Nếu có ai biết plz này giúp tôi giải quyết vấn đề này.

+0

có thể có liên quan đến $ config ['csrf_protection'] = TRUE; mà bạn tìm thấy trong tập tin cấu hình codeigniter – gianni

+0

Cảm ơn bạn đã bình luận ur. Tôi đã thử nhưng không làm việc. – Raj

Trả lời

0

Vâng, tôi không biết nó điều này sẽ giúp bạn, nhưng đây là cách tôi đang làm nó:

tôi có chức năng này trong một dịch vụ:

saveFunction: function(obj, user, securityToken){ 
      return $http({ 
      method: 'POST', 
      url: 'index.php/controller/saveFUnction', 
      data: $.param({ 
       postID: obj.id , 
       csrfTokenName: securityToken, 
       postName: obj.name, 
       ..... 
       userID: user.id     
      }), 
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' } 
     }); 
    } 

nếu csrfToken bạn đang hoạt động trong cấu hình codeigniter, bạn cần chuyển nó vào form của bạn, như trường kiểu ẩn. Ngoài ra, bạn phải đặt loại nội dung trong tiêu đề.

Các vấn đề liên quan