错误:无法获得财产'来电'未定义或空引用

时间:2017-02-08 13:02:13

标签: angularjs

我正在使用AngularJs。从controller.js获取数据到service.js时,我收到错误。以下是使用的代码:

// controller.js

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;

template<typename T>
T getRandomItem(vector<T>& items){ // note that we should pass by reference to prevent extra copy
    if(items.size() == 0) return T();
    srand(time(NULL));
    int i = rand()%items.size()+0;
    return items.at(i);
}

int main() {
    vector<string> v;
    v.push_back("foo");
    v.push_back("bar");
    string item = getRandomItem(v);
    std::cout << item;
    return 0;
}    

在Service.js

function get_all_opened($mailgun, $domain, $list_address, $items, $page_advance = false, $next_url = false) 
{

    $end_point = "{$domain}/events";
    if($page_advance) {
        $end_point = "{$domain}/events/{$next_url}";
    }

    //API Call
    $stats = $mailgun->get($end_point, array(
        'event' => 'opened',
        'limit' => 25,
        'list' => $list_address
    ));

    $item_count = count($stats->http_response_body->items);

    if($item_count > 0) {
        $items[] = $stats->http_response_body->items;
    }
    if($item_count < 25) {
        var_dump($items); //Correct result set
        return $items; //Always NULL
    }

    if($item_count > 24) {
        $next_parts = explode ('/', $stats->http_response_body->paging->next);
        $next_url = end($next_parts);
        get_all_opened($mailgun, $domain, $list_address, $items, true, $next_url);
     }
}

$items = array();
get_all_opened($mailgun, $domain, $list_address, $items);

如何解决这个问题?感谢

2 个答案:

答案 0 :(得分:0)

您正在创建两个不同的模块:

  • 创建控制器时创建第一个模块export function count(){ let a; // Declared outside the blocks if (counter > 3){ a = 'big'; } else { a = 'small'; } return a; }
  • 创建服务时会创建另一个模块testApp.controllers

所以控制器和服务永远不属于同一个模块!

尝试附加到相同的testApp.services模块,如下所示:

<强> app.js

testApp

controller.js

// With [] means to create a new module.
angular.module('testApp', []); 

<强> service.js

// Without [] means to retrieve an existing module.
angular.module('testApp').
controller('testController', function($scope, testAPIService, $timeout, $window, $location, $anchorScroll, $http) {

  $scope.show = function() {

    testAPIService.getDataSummary().success(function(response) {
      console.log(response);

    }).error(function(response) {
      alert(response.responseText);
    });

  }
});

<强>的index.html

并将您的// Without [] means to retrieve an existing module. angular.module('testApp'). factory('testAPIService', ['$http', function($http) { var testAPIService = {}; testAPIService.getDataSummary = function() { var request = { url: urlBase + 'GetDataSummary', method: 'Get', headers: { 'accept': 'application/json' } } return $http(request); }; return testAPIService; }]); 指令更改为指向ng-app模块

testApp

答案 1 :(得分:0)

这可能是在 angularjs 文件之前加入任何应用javascript文件的结果。

确保在其余的应用文件之前包含angularjs文件。