• AngularJS学习篇(二十)


    AngularJS 包含

    在 AngularJS 中,你可以在 HTML 中包含 HTML 文件。

    使用 AngularJS, 你可以使用 ng-include 指令来包含 HTML 内容:

    <body ng-app="">
     
    <div ng-include="'test.htm'"></div>
     
    </body>

    test.htm 文件代码:

    <h1>菜鸟教程</h1>
    <p>这是一个被包含的 HTML 页面,使用 ng-include 指令来实现!</p>

    ng-include 指令除了可以包含 HTML 文件外,还可以包含 AngularJS 代码:

    <!DOCTYPE html>
    <html>
    <meta charset="utf-8">
    <script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
    <body>
    
    <div ng-app="myApp" ng-controller="sitesCtrl"> 
      <div ng-include="'sites.htm'"></div>
    </div>
     
    <script>
    var app = angular.module('myApp', []);
    app.controller('sitesCtrl', function($scope, $http) {
        $http.get("sites.php").then(function (response) {
            $scope.names = response.data.records;
        });
    });
    </script>
    
    <p>AngularJS 代码包含在 "sites.htm" 文件中。</p>
    
    </body>
    </html>

    sites.htm 文件代码:

    <table>
    <tr ng-repeat="x in names">
    <td>{{ x.Name }}</td>
    <td>{{ x.Url }}</td>
    </tr>
    </table>

    跨域包含

    默认情况下, ng-include 指令不允许包含其他域名的文件。

    如果你需要包含其他域名的文件,你需要设置域名访问白名单:

    <!DOCTYPE html>
    <html>
    <meta charset="utf-8">
    <script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
    <body ng-app="myApp">
     
    <div ng-include="'http://c.runoob.com/runoobtest/angular_include.php'"></div>
     
    <script>
    var app = angular.module('myApp', [])
    app.config(function($sceDelegateProvider) {
        $sceDelegateProvider.resourceUrlWhitelist([
            'http://c.runoob.com/runoobtest/**'
        ]);
    });
    </script>
    <p>你需要设置服务端允许跨域访问,设置方法可参考 <a target="_blank" href="/w3cnote/php-ajax-cross-border.html">PHP Ajax 跨域问题最佳解决方案</a></body>
    </html>

    此外,你还需要设置服务端允许跨域访问,设置方法可参考:PHP Ajax 跨域问题最佳解决方案。

    <?php
    // 允许所有域名可以访问
    header('Access-Control-Allow-Origin:*');
     
    echo '<b style="color:red">我是跨域的内容</b>';
    ?>
  • 相关阅读:
    Java中类的继承
    信号量、PV原语及其应用
    Python-subprocess执行命令并将输出劫持实现实时记录到日志
    Python-logging模块定制格式描述符实现定长日志等级
    Python-logging模块实现同时向控制台和文件打印日志
    SpringBoot学习笔记(二)
    Kubernetes学习日记(四)
    Kubernetes学习日记(三)
    SpringBoot学习笔记(一)
    Kubernetes学习日记(二)
  • 原文地址:https://www.cnblogs.com/dehuachenyunfei/p/6756421.html
Copyright © 2020-2023  润新知