【问题标题】:Angularjs and Laravel service is not call?Angularjs 和 Laravel 服务不调用?
【发布时间】:2015-08-05 06:21:27
【问题描述】:

我正在使用 Angularjs 和 Laravel5。当我调用自动完成文本框时,服务调用没有响应。我的代码是......

userdefined.js

var app=angular.module('app',[]);
  app.controller('ctrl',function($scope,$http){

      $http.get("http://localhost:85/xamppserver/CODE_8JULY2015/public/front/state/w")
       .success(function(response) {
        $scope.availableTags = response;
        });  




    $scope.complete=function(){
      console.log($scope.availableTags);
    $( "#tags" ).autocomplete({
      source: $scope.availableTags
    });
    } 

  });

index.blade.php

<div class="form-group" ng-app="app" ng-controller="ctrl">
                    <label>State</label>                    
                    <input type="text" class="form-control" placeholder="State" id="tags" ng-keyup="complete()">                                        
                </div>

route.php

Route::get('state/{statename}', ['as' => 'customer','uses' => 'CustomerController@selectstate']);

CustomerController.php

public function selectstate($statename)
    {       
        //$customer = new Customer();               
        //$result = $customer->where('client_state','like', '%'.$Filter.'%')->get(array('client_state'));

        $state = new State();       
        $result = $state->where('state_name','like', '%'.$statename.'%')->get(array('state_name'));

        header('Content-type: application/json');
        echo json_encode($result);
    }

如何使用angularjs调用服务?

【问题讨论】:

  • 当您将该 URL 放入浏览器“localhost:85/xamppserver/CODE_8JULY2015/public/front/state/w”时,您会得到预期的响应吗?
  • 是的,我收到了 json 格式的回复,例如 [{"state_name":"West Bengal"}]
  • JS 控制台有错误吗?请求是否已发送?你得到回应了吗?在成功处理程序中添加 console.log(response) 并粘贴结果

标签: php angularjs laravel-5


【解决方案1】:

你不能把 $http.get 放在一个函数中然后调用它。

var app=angular.module('app',[]);
  app.controller('ctrl',function($scope,$http){
getstate();
function getstate(){
      $http.get("http://localhost:85/xamppserver/CODE_8JULY2015/public/front/state/w")
       .success(function(response) {
        $scope.availableTags = response;
        });  
}



    $scope.complete=function(){
      console.log($scope.availableTags);
    $( "#tags" ).autocomplete({
      source: $scope.availableTags
    });
    } 

  });

【讨论】:

    猜你喜欢
    • 2023-03-04
    • 2017-10-11
    • 1970-01-01
    • 2016-02-13
    • 2016-10-10
    • 2018-01-28
    • 2015-01-22
    • 2014-06-23
    • 2018-03-03
    相关资源
    最近更新 更多