在嵌套视图中以角度加载json数据?

时间:2014-11-05 12:43:00

标签: angularjs

我是Angular的新手并试图找出嵌套视图:

我有一个json文件,我想在其中一个嵌套视图中显示它;我没有得到任何结果:

应用:

var myApp = angular.module('myApp',[]);

myApp.config(['$routeProvider',

    function($routeProvider){

        $routeProvider.
            when('/tab1', {
                templateUrl: 'tab1.html',
                controller: 'tab1'
            }).
            when('/tab2',{
                templateUrl: 'tab2.html',
                controller: 'tab2'
            }).
            when('/tab3',{
                templateUrl: 'tab3.html',
                controller: 'tab3'
            }).
            otherwise({
                redirectTo: '/tab1'
            });
    }]);

myApp.controller('tab1', function($scope, $http){
    $scope.message = 'This is Tab1 space';
});

myApp.controller('tab2', function($scope){
    $scope.message = 'This is Tab2 space';
});

myApp.controller('tab3', function($scope){

    $http.get('data.json')
       .success(function(data){
          $scope.cars = data;                
        });
});

我可以显示tab1和tab2文本,但是tab3有json数据..

如何配置tab3控制器以便能够在data3视图中显示json数据?

myApp.controller('tab3', function($scope){

$http.get('data.json')
       .success(function(data){
          $scope.cars = data;                
        });
});

HTML:

<html lang="en" ng-app="myApp">
  <head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
    <script src="myApp.js"></script>
  </head>
  <body>
                <ul>
                    <li>
                    <a href="#tab1">tab1</a>
                    </li>
                    <li>
                    <a href="#tab2">tab2</a>
                    </li>
                    <li>
                    <a href="#tab3">Check Cars</a>
                    </li>
                </ul>

                <div ng-view></div>

  </body>
</html>

和tab3.html

  <span ng-repeat="car in cars">
 {{car.Brand}}
 </span>

如何在tab3中调用json数据?

Exp:http://plnkr.co/edit/medE55ZlFYBtWJExrxjU?p=preview

提前谢谢你!

1 个答案:

答案 0 :(得分:1)

$http控制器中添加tab3依赖项,如下所示,

myApp.controller('tab3', function($scope,$http){