【问题标题】:How to get apiurl in angularjs and codeigniter如何在angularjs和codeigniter中获取apiurl
【发布时间】:2015-04-15 13:14:17
【问题描述】:

我的 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 的 apiurl 路径? 我正在使用 CodeIgniter 作为后端。

【问题讨论】:

  • 这可能不是你想要的,但我有时会添加一个脚本元素,它只是在加载其余源代码之前在 html 的正文中设置一个全局变量。 &lt;script&gt;var apiUrl = '{$serverSideVariableForUrl}';&lt;/script&gt;
  • define var apiurl =url 到 ci 文件夹;在你里面 var registeruserRest = angular.module('registeruserRest', [])

标签: javascript php angularjs codeigniter


【解决方案1】:

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

    <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/";

【讨论】:

  • 默认情况下不设置 apiurl 它在服务器上完美地采用服务器路径。但是当我在 localhost 上执行此操作时仍然采用服务器路径。
  • 如果不在 localhost 上设置 apiurl 就不能获取默认路径吗??
猜你喜欢
  • 2023-03-23
  • 2017-11-09
  • 1970-01-01
  • 1970-01-01
  • 2014-03-31
  • 1970-01-01
  • 2014-08-07
  • 1970-01-01
  • 2012-07-31
相关资源
最近更新 更多