ng-include不适用于基本的角度1代码

时间:2015-09-30 23:36:17

标签: angularjs

好的,有很多类似的问题,我经历了一些让我更加困惑,顺便说一句,我开始在2周左右开始学习角度。

  1. 在浏览器中,/ mathath / status.html工作正常,它将“testJson”发送到节点服务器,然后 将数据显示到网页

  2. 现在我有/mypath/index2.html,其中ng-include status.html,它不会 在服务器代码中触发“testJson”

  3. status.html

    <!DOCTYPE html>
    <html ng-app="HelloModule">
      <head>
          <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
      </head>
    
      <body  ng-controller="HelloCtrl">
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.6/
    angular.js"></script>
        <script src="statusApp.js"></script>
    
    
        <table>
          <tr>
            <th>Code</th>
            <th>Country</th>
            <th>Population</th>
          </tr>
    
    
          <tr ng-repeat="country in countries | orderBy: 'code' ">
            <th>{{country.code}}</th>
            <th>{{country.name}}</th>
            <th>{{country.population}}</th>
          </tr>
        </table>
    
      </body>
    
    </html>
    

    statusApp.js

    var app = angular.module('HelloModule', []);
    
    app.controller('HelloCtrl', function($scope, HelloService) {
            //$scope.countries = [{code : "US", name : "United States", population : "11223344"}];
    
        HelloService.getJson().then(function(result) {
            $scope.countries = result;
        }, function(error) {
            alert("Error");
        } );
    });
    
    
    app.factory('HelloService', function($http, $q) {
    
      return {
         getJson:  function() {
            var deferred = $q.defer();
    
            var browserProtocol = 'http';
            var port = ':1234';
            var address = 'localhost';
            var server = browserProtocol + '://' + address;
            var url = server + port + '/testJson';
    
            //$http.get('http://localhost:7778/testJson').success(function(data) {
            $http.get(url).success(function(data) {
                deferred.resolve(data);
            }).error(function(){
                deferred.reject();
            }); 
    
          return deferred.promise;
        }
      }
    });
    

    index2.html无效, 我没有看到这一行      console.log(“返回Json数据”); 在服务器中触发

    <!DOCTYPE html>
    <html ng-app>
       <head>
           <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
       </head>
    <body>
    
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.6/angular.
    min.js"></script>
    
    <div class="container">
        <div ng-include="" src="'status.html'"></div>
    </div>
    
    </body>
    </html>
    

    服务器端

    app.get('/testJson', function(req, res)
    {
            console.log("returning Json data");
        res.json( [{code : "US", name : "United States", population : "11223344"
    }] );
    });
    

1 个答案:

答案 0 :(得分:1)

您要包含的status.html是:

  <table ng-controller="HelloCtrl" >
      <tr>
        <th>Code</th>
        <th>Country</th>
        <th>Population</th>
      </tr>


      <tr ng-repeat="country in countries | orderBy: 'code' ">
        <th>{{country.code}}</th>
        <th>{{country.name}}</th>
        <th>{{country.population}}</th>
      </tr>
    </table>

对吗?

<强> 修改

ng-include 的工作原理如下:

<div class="container">
    <div ng-include="'status.html'" ></div>
</div>