2016-04-24 43 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是对象对象 –

相关问题