Javascript 自调用函数在 angular ionic 中不起作用

Javascript self invoking function not working in angular ionic

控制器里面的代码是:

(function(){
    $scope.show();
    profile.friend_requests_to_me_service(loggedInUser.id).then(function(data){
        console.log(data);
        $scope.friend_requests_to_me = data.data.friend_request_to_me;
        $scope.friends = data.data.people_you_may_know;
        $scope.hide();
    });   
})();

如您所见,此函数正在调用服务配置文件,service.The 问题的一个方法是此调用函数不是 working.The 错误: TypeError: (intermediate value)(...) is not a function。在 plunker 中,我检查了一个微不足道的自我调用 function.Here 是 link:http://jsfiddle.net/Lvc0u55v/4582/。 这个微不足道的自调用函数是 working.But 我无法理解为什么我的实际应用程序自调用函数不是 working.Thank 你的时间。

目前的工作版本(非自调用)是: $scope.friendship_requests_to_me_function = 函数(){ $scope.show(); profile.friend_requests_to_me_service(loggedInUser.id).then(函数(数据){ console.log(数据); $scope.friend_requests_to_me = data.data.friend_request_to_me; $scope.friends = data.data.people_you_may_know; $scope.hide(); });<br> } $scope.friendship_requests_to_me_function();

很可能您的 js 语句中缺少一个“;”在末尾。

尝试添加一个“;”在你自己调用函数之前.

阅读更多here

We end up passing the second function as an argument to the first function and then trying to call the result of the first function call as a function. The second function will fail with a "... is not a function" error at runtime.