AngularJS从视图中调用HtppGet方法给出了无限循环

时间:2016-10-11 12:50:11

标签: php angularjs

所以我有一个包含两个 http get方法的控制器,看到我不想创建一个完全相同但带参数的第二个控制器。

查看:

<div ng-init="getContacts(<?php echo $user->id ?>)';">
                <div class="cblock">

                        contact
                    </h2>
                </div>
                <div ng-controller="ContactListCtrl"> 

                    <div>
                        <div ng-repeat="contact in contacts track by $index>
                            {{contact.firstname}} 
                        </div>
                    </div>

                </div>
            </div>

AngularJS:

angularControllers.controller('ContactListCtrl', function ($scope, $http) {
$scope.company_id;    
$scope.contacts;
$scope.allcontacts;    
$scope.getContacts = function ($id) {
    $http({
        method: 'GET',
        url: 'api/contact.php?user_id='+$id
    }).then(function (response) {
        $scope.contacts = response.data;
    }, function (response) {
        alert(response.data, response.status);
    });
};
$scope.getAllContacts = function () {
    $http({
        method: 'GET',
        url: 'api/contact.php'
    }).then(function (response) {
        $scope.allcontacts = response.data;
    }, function (response) {
        alert(response.data, response.status);
    });
};

contact.php

<?php 

require_once('api_init.php');


if(isset($_GET['id']))
{
    $company = Company::find_by_id($_GET['id']);
    header('Content-Type: application/json');
    echo json_encode($company);
}
if(isset($_GET['company_id']))
{
    $contact = Contact::find_all_withcompanyID($_GET['company_id']);
    header('Content-Type: application/json');
    echo json_encode($contact);   
}
else 
{
     $contact = Contact::find_all();
    header('Content-Type: application/json');
    echo json_encode($contact);   
}

如何在两个函数中找到一个并检索要在div中使用的数据?现在ng-init中的getContacts()会导致无限错误,我想是因为跟踪脏了?

0 个答案:

没有答案