Angularjs[26] - 自定义指令(2)(templateUrl 属性)


  • templateUrl: 加载模板所要使用的 URL。
  • 可以加载当前模板内对应的 text/ng-template script id。
  • 在使用 chrome 浏览器时,“同源策略”会阻止 chorme 从 file:// 中加载模板,并显示一个“ Access-Control-Allow-Origin ”不允许源为 null,可以把项目放在服务器上加载,或者给 chorme 设置一个标志位,命令为:chorme-allow-file-access-from-files。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <div ng-app="myApp">

        <script type="text/ng-template" id="customTags2">
            <div>
                hello {{name}}
            </div>
        </script>

        <div ng-controller="firstController">
            <custom-tags></custom-tags>
            <custom-tags2></custom-tags2>
        </div>

    </div>

<script type="text/javascript" src="../../vendor/angular/angularjs.js"></script>
<script type="text/javascript" src="app/index.js"></script>
</body>
</html>
 <div>{{name}}</div>
var myApp = angular.module('myApp',[])
.directive('customTags',function () {
    return{
        restrict: 'ECAM',
        templateUrl:'tmp/other.html',
        replace: true
    }
})
.directive('customTags2',function () {
    return{
        restrict: 'ECAM',
        templateUrl:'customTags2',
        replace: true
    }
})
.controller('firstController',['$scope',function ($scope) {
    $scope.name = 'Alrale';
}]);

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM