javascript - Angular路由加载视图后,如何实现立即初始化请求数据?
巴扎黑
巴扎黑 2017-04-11 12:24:52
0
2
456
  • HTML代码(home.html)

<html>
  <body>
    <!-- 目前我只知道通过点击事件来触发加载-->
    <!-- 如何实现一跳转到视图,就调用$scope.getHomeData()方法呢? -->
    <a href="javascript:void(0);" ng-repeat="getHomeData()">
  </body>
</html>
  • JS代码

//配置主页的路由
mainApp.config(function($stateProvider, $urlRouterProvider) {
  //默认指向404
  $urlRouterProvider.otherwise("/404");
  //配置路由
  $stateProvider.state('home', {
    url: '/home',
    templateUrl: 'home.html',
    controller : function($scope,$http){
      //定义获取首页相关数据的方法
      $scope.getHomeData = function(){
        $http({
          url : '/getHomeData',
          method : 'POST'
        }).then(function(){
            //请求成功
        },function(){
            //请求失败
        })
      };
    }
  }).state('404',{
    url: '/home',
    templateUrl: 'home.html'
  });
});
  • 麻烦大神看一下伪代码,多多指教

巴扎黑
巴扎黑

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!