[Angularjs]ng-include 包含

写在前面

有时我们需要动态的创建一些标签,或者在收到服务端返回的json,创建一些标签然后找到页面上的元素,通过innerHTML写入到页面上面。angularjs也为我们提供了一种比较方便操作方式,ng-include。

ng-include:可以在html中包含html文件。

一个例子

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="myapp">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="../Scripts/angular.min.js"></script>
    <script>
        var app = angular.module('myapp', []);
        app.controller("myCtrl", function ($scope) {
            //控制器
        });
    </script>
</head>
<body>
    <div ng-controller="myCtrl">
        <div ng-include="'test.html'"></div>
    </div>
</body>
</html>

test.html

<h1>
    这是个内容页面
</h1>

注意

ng-include包含的页面需要加上单引号。

结果

原文地址:https://www.cnblogs.com/wolf-sun/p/5435024.html