2016-08-10 9 views
1
var init = function(){ 
    $scope.getAllFriends($rootScope.rootName) 
     .then(function(data) { 
      console.log(data); 
     }, function(err) { 
      //error 
     }); 
} 
init(); 
$scope.getAllFriends = function(name){ 
    return friendService.getAllfriends(name) 
} 

anglejsに新しいですが、ページの読み込み時に関数を呼び出そうとしていますが、エラーが発生しています。誰でも私が間違っている場所を修正することができます。AngularJS:ページの読み込み時に関数を呼び出す方法

はTypeError:$ scope.getAllFriendsあなたはまだ関数を呼び出すしようとしている機能

+0

あなたはNG-INIT = "は、init()" htmlタグの角度ディレクティブを使用することができます(https://docs.angularjs.org/api/ng/directive/ngInit)[参照] –

+0

ショー:このようにさらに援助のためのあなたのHTMLコード – AranS

答えて

2

ない宣言:それは実行のinit getAllFriendsは、一度に宣言されていません。

したがって、 "TypeError:$ scope.getAllFriendsは関数ではありません"と表示されます。機能の代わりに、オブジェクトの名前

var init = function() { 
     $scope.getAllFriends() 
      .then(function (data) { 
       console.log(data); 
      }, function (err) { 
       //error 
      }); 
    } 

$scope.getAllFriends = function (name) { 
     return friendService.getAllfriends(name) 
} 
init(); 
+0

ありがとう..それは働いた.. – Raghav

0

用途:

ではなく、このようにしてみてください。

function getAllFriends(name){...} 
関連する問題