2016-04-24 44 views
0

你好我是angularJS新手,我嘗試在angularJS中使用onload函數。我創建了一個控制器,並在該控制器中使用$ scope.init獲取記錄形式的mysql數據庫。我正在使用laravel框架。 這是我的視圖代碼如何使用laravel框架在angular js中使用onload函數?

<div class="navbar-collapse collapse" id="navbar" ng-controller="NotificationsCtrl"> </div> 

這是我的控制器代碼

app.controller('NotificationsCtrl', function($scope, $http, $window, $timeout){ 

var user_id = sessUser.user_id; 
alert(user_id); 
$scope.init = function() 
{ 
    $scope.getNotificationList(); 
}; 

$scope.getNotificationList = function() 
    { 
    $scope.dataLoading = true; 
    $http.get(siteUrl+"notification/getNotificationList/"+user_id).then(function(response) 
    { 
     alert(response); 
    }); 
    };  

});

這是我的路線

Route::get('notification/getNotificationList/{user_id}', '[email protected]'); 

這是我laravel控制器

public function getNotification($id) 
{ 
    echo $id; 
} 

請幫助我,我想我犯了一個錯誤。

+0

什麼是sessUser? –

+0

你還在哪裏調用'$ scope.init'函數。也是爲什麼它是範圍函數?範圍函數只用於從視圖調用時使用。 –

+0

其會話的用戶ID –

回答

0
$scope.init = function() 
{ 
    $scope.getNotificationList(); 
}; 

$scope.init(); 
0

你需要一個像this.You不打電話給你的init method.Also不需要寫範圍功能,除非與視圖中使用寫您的控制器。

app.controller('NotificationsCtrl', function($scope, $http, $window, $timeout){ 

var user_id = sessUser.user_id; 
alert(user_id); 
var getNotificationList = function() 
{ 
    $scope.dataLoading = true; 
    $http.get(siteUrl+"notification/getNotificationList/"+user_id).then(function(response) 
    { 
     alert(response); 
    }); 
};  

var init = function() 
{ 
    getNotificationList(); 
}; 

init(); //call here init 
} 
+0

當我加載該頁面響應顯示[目標對象] –

+0

CONSOLE.LOG響應看到實際的結果 –

+0

我嘗試你給的代碼,但後負荷頁respose是對象對象 –

相關問題