如何在angularjs和codeigniter中获得apiurl

时间:2015-04-15 13:14:17

标签: javascript php angularjs codeigniter

我的HTML代码是

<div class="page">

    <div class="row">
    <div class="col-md-9">
        <section class="panel panel-default">
            <div class="panel-heading"><strong><span class="glyphicon glyphicon-th"></span>Show Registered User</strong>

            </div>

            <div class="panel-body">
              <!--DEmo###############################################-->

              <!--DEmo###############################################-->
              <div class="bot">
              <input type="search" name="search" ng-model="search" placeholder="Search"/>
           <a href="#/registeruser/createregisteruser" class="right btn btn-primary">Create registeruser</a>
            </div><br>
               <div class="msg"> {{usermessage}} </div>       
              <div ng-show="visibletable">

                <table class="table table-bordered table-striped">
                    <thead><tr class="bg-dark">
                        <th class="text-center">ID</th>
                        <th class="text-center">Name</th>
                        <th class="text-center">Phone Number</th>
                        <th class="text-center">gps Location</th>
                        <th colspan="2" class="text-center">OPERATIONS</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr ng-repeat="value in find|filter:search">
                        <td class="text-center">{{value.id}}</td>
                        <td class="text-center">{{value.name}}</td>
                            <td class="text-center">{{value.phoneno}}</td>
                            <td class="text-center">{{value.gpslocation}}</td>
                            <td class="text-center"><a href="#/registeruser/delete/{{value.id}}" class="btn btn-primary"><i class="fa fa-times"></i></a></td>
                            <td class="text-center"><a href="#/registeruser/edit/{{value.id}}" class="btn btn-primary">
                            <i class="fa fa-pencil-square-o"></i></a></td>
                        </tr>
                    </tbody>
                </table> 
                </div>

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

我的控制器是

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Registration extends CI_Controller {

/**
 * Index Page for this controller.
 *
 * Maps to the following URL
 *      http://example.com/index.php/welcome
 *  - or -  
 *      http://example.com/index.php/welcome/index
 *  - or -
 * Since this controller is set as the default controller in 
 * config/routes.php, it's displayed at http://example.com/
 *
 * So any other public methods not prefixed with an underscore will
 * map to /index.php/welcome/<method_name>
 * @see http://codeigniter.com/user_guide/general/urls.html
 */

public function create()
{
    $name=trim($this->input->get_post("username"));
    $phoneno=trim($this->input->get_post("phoneno"));
    $gpslocation=trim($this->input->get_post("gpslocation"));
    if($name=="")
    {
        echo "Name is Required";
    }
    else if($phoneno=="")
    {
        echo "Phone Number is Required";
    }
    else 
    {
        $data['json']=$this->registrationmodel->insert($name,$phoneno,$gpslocation);
        $this->load->view('json',$data);
    }

}
public function update()
{
    $id=trim($this->input->get_post("id"));
    $name=trim($this->input->get_post("name"));
    $phoneno=trim($this->input->get_post("phoneno"));
    $gpslocation=trim($this->input->get_post("gpslocation"));
    if($name=="")
    {
        echo "Nmae is Required";
    }
    else if($phoneno=="")
    {
        echo "Phone Number is Required";
    }
    else 
    {
    $data['json']=$this->registrationmodel->update($id,$name,$phoneno,$gpslocation);
    $this->load->view('json', $data);
    }
}
public function find()
{
    $data['json']=$this->registrationmodel->viewall();
    $this->load->view('json', $data);
}
public function findone()
{
    $id=trim($this->input->get_post("id"));
    $data['json']=$this->registrationmodel->viewone($id);
    $this->load->view('json', $data);
}
public function delete()
{
    $id=trim($this->input->get_post("id"));
    $data['json']=$this->registrationmodel->deleteone($id);
    $this->load->view('json', $data);
}
public function filterdata()
{
    $search=trim($this->input->get_post("search"));
    $data['json']=$this->registrationmodel->filterdata($search);
    $this->load->view('json', $data);
}
}

我的休息服务是

var registeruserRest = angular.module('registeruserRest', [])

.factory('registeruserRest', function ($http) {

return{
    create: function(data){
        console.log();
       return $http.get(apiurl+"/index.php/registration/create",{params:data});
    },
    find: function(){
        console.log();
        return $http.get(apiurl+"/index.php/registration/find");
    },
    findoneregisteruser: function(id){
        console.log();
        return $http.get(apiurl+"/index.php/registration/findone?id="+id,{})
    },
    deleteregisteruser: function(id){
        return $http.get(apiurl+"/index.php/registration/delete?id="+id,{});
    },
    updateregisteruser: function(data){
        return $http.get(apiurl+"/index.php/registration/update",{params:data});
    },
    filter: function(data){
        return $http.get(apiurl+"/index.php/registration/filterdata",{params:data});
    }
}

});

我已将此代码从服务器下载到localhost。 当我在localhost上运行此代码时,其余服务仍在运行,/ response来自在线服务器。

那么,如何为localhost设置apiurl路径? 我正在使用CodeIgniter作为后端。

1 个答案:

答案 0 :(得分:0)

你可以用 //在您的页面中定义

    <link id="RootPAth" href="/DEFINE YOUR PATH HERE/"/>

    angular.module("app.services", [])
        .config(["$provide", function ($provide) {
            $provide.value("apiRoot", $("#RootPAth").attr("href"));
        }]);

    angular.module("app.services").factory("myAdminSvc", ["apiRoot",   function (apiRoot) {
var apiAdminRoot = apiRoot + "your path/";