返回到文章

采纳

编辑于

angularJs利用Directive实现View复用

AngularJs
AngularJS
笔记

Angularjs的MVC是借助于$scope实现的。

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <script src="https://www.orchome.com/user/js/angular-1.5.8.min.js"></script>
</head>
<body ng-app="app">
    <hello></hello>
    <hello></hello>
</body>

<script>
    var app = angular.module("app", []);
    app.directive("hello", function () {
        return {
            restrict: "E",
            template: '<div>Hi everyone!</div>',
            replace: true
    }
    });
</script>
</html>

打开页面:

Hi everyone!
Hi everyone!

在线运行