[英]JavaScript inside Angular's (1.4.7) ng-view doesn't work
我有一个进入 ng-view 的部分页面,我有与该页面无关的 JS 代码,所以我希望它只包含在该页面中,因为我只需要它并避免开销我的 index.html,我尝试使用ng-bind-html
注入,但无效。
这里有一些代码可以帮助理解发生了什么。
索引.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<base href="/">
<title>Node and Angular App</title>
<!-- CSS -->
<link rel="stylesheet" href="libs/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="css/style.css">
<!-- JS -->
<script src="libs/angular/angular.min.js"></script>
<script src="libs/angular-route/angular-route.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular-sanitize.min.js"></script>
<!-- ANGULAR CUSTOM -->
<script src="js/controllers/TestCtrl.js"></script>
<script src="js/appRoutes.js"></script>
<script src="js/app.js"></script>
</head>
<body ng-app="sampleApp">
<div class="container">
<nav>
<a href="/test">Test</a>
</nav>
<!-- ANGULAR DYNAMIC CONTENT -->
<div ng-view></div>
</div>
</body>
</html>
测试控件.js
angular.module('TestCtrl', ['ngSanitize']).controller('TestController', function($scope) {
var str = '<h1>this shows</h1><script>alert("hello")</script><h1>this doesn\'t show</h1>';
$scope.content = function() { return str; }
});
测试.html
<div class="jumbotron" ng-bind-html="content()"></div>
appRoutes.js
angular.module('appRoutes', []).config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
$routeProvider
.when('/test', {
templateUrl: 'views/test.html',
controller: 'TestController'
})
$locationProvider.html5Mode(true);
}]);
应用程序.js
var app = angular.module('sampleApp', ['ngRoute', 'appRoutes', 'TestCtrl']);
我不完全理解您的问题,但如果您想将 html 注入视图,请尝试将 ngSanitize 添加到模块中的依赖项列表中,您可以拥有:
controller("mainCtrl", ['$sce', function($sce){
//function that returns trusted html
this.htmlStuff = function(){
return $sce.trustAsHtml("<p>Html String</p>");
}
}]);
那么在你看来,你可以有
<div ng-controller='mainCtrl as main'>
<div ng-bind-html="main.htmlStuff()"></div>
</div>
如果这不是你的意思,那么你必须改写这个问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.