【问题标题】:How to use onload function in angular js with laravel framework?如何在带有laravel框架的角度js中使用onload函数?
【发布时间】:2016-04-24 17:11:58
【问题描述】:

您好,我是 angularJS 的新手,我尝试在 angularJS 中使用 onload 函数。我创建了一个控制器,并在该控制器中使用 $scope.init 从 mysql 数据库中获取记录。我正在使用 laravel 框架。 这是我的视图代码

 <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}', 'ProjectController@getNotification');

这是我的 laravel 控制器

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

请帮助我,我认为我犯了一个错误。

【问题讨论】:

  • 还有你在哪里调用$scope.init函数。还有为什么它是作用域函数?作用域函数只能在视图调用时使用。

标签: angularjs laravel


【解决方案1】:

您可以在标记上使用“ng-init”指令,这是文档:

http://www.w3schools.com/angular/angular_directives.asp

只需在 ng-init 中调用您的 init 函数,或在控制器末尾调用 $scope.init() 即可

【讨论】:

    【解决方案2】:
    $scope.init = function() 
    {
        $scope.getNotificationList();
    };
    
    $scope.init();
    

    【讨论】:

      【解决方案3】:

      你需要像这样编写你的控制器。你没有调用你的 init 方法。也不需要编写作用域函数,除非使用视图。

      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
      }
      

      【讨论】:

      • 当我加载这个页面响应显示 [object object]
      • Console.log 响应以查看实际结果
      • 我试过你给代码,但加载页面后响应是对象对象
      • 控制台是否有错误。接下来你是否要求访问 larval.chk 这些
      • @SandipKakade 使用console.log 在控制台中查看整个对象。
      猜你喜欢
      • 2022-10-07
      • 1970-01-01
      • 2023-03-25
      • 2023-03-25
      • 2019-07-11
      • 1970-01-01
      • 1970-01-01
      • 2018-04-28
      • 2018-06-03
      相关资源
      最近更新 更多