在加载的html页面中使用angular脚本(通过ajax)



我有两个HTML页面,其中包括Jquery+Angularjs+一些脚本。当我的第一个html加载时,jquery脚本将通过ajax加载其他html页面,第二个html页面包含一些不再工作的angular脚本。

有什么解决方案吗?即使需要更改ajax的脚本,也没有问题

检查此project

正如你所描述的,我只能建议你这些东西-

当您加载第一个html页面时,您必须在中加载有角度的脚本。

因为第二个html页面是由脚本加载的,所以第二个html页面此时不会加载角度脚本。

您可以添加一个.js文件,脚本代码将在其中,该js文件将包含在第一个html页面中。

您可以通过以下方式尝试

将控制器移至index.html

index.html的body标签中添加应用程序声明

<body ng-app="myApp">

车身标签内包括对other.html 的引用

 <div ng-include="'other.html'"></div> 

在other.html中,您只需要以下代码

<div ng-controller="myCtrl">
    Number {{yourName}}!
</div>

Plunker演示

终于找到了我想要的

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
  <p ng-bind-html="myText"></p>
</div>
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope, $http, $sce) {
    $http.get("page2.html").then(function (response) {
       $scope.myText = $sce.trustAsHtml(response.data);
    });
});
</script>
</body>
</html>

最新更新