简体   繁体   English

在同一页面中加载不同html页面的内容

[英]load the content of different html pages in the same page

When user selects the link, i want to load the content of other html inside the same page as per user slection. 当用户选择链接时,我想根据用户选择在同一页面内加载其他html的内容。 The links provided are inside a controller(myController). 提供的链接位于控制器(myController)内部。 How to load the red.html and green.html based on the user selection. 如何根据用户选择加载red.html和green.html。 The below js code works if the links are not inside the ng-controller. 如果链接不在ng-controller内部,则下面的js代码有效。

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.js"></script>

<body ng-app="myApp">

<div ng-controller="myController">
<a href="#!red">Red</a>
<a href="#!green">Green</a>
<div ng-view></div>
</div>
<script>
var app = angular.module("myApp", ["ngRoute"]);
app.config(function($routeProvider) {
    $routeProvider
    .when("/", {
        templateUrl : "main.htm"
    })
    .when("/red", {
        templateUrl : "red.html"
    })
    .when("/green", {
        templateUrl : "green.html"
    })
    .when("/blue", {
        templateUrl : "blue.html"
    });
});
</script>

</body>
</html>

PS: I want the links to be inside ng-controller="myController" and based on the user selection i want to load the html page in the same page. PS:我希望链接位于ng-controller="myController"并且基于用户选择,我想将HTML页面加载到同一页面中。 If i remove ng-controller from div, it is loading the content but i want the links to be inside the controller as shown in the above code. 如果我从div中删除ng-controller,则它正在加载内容,但我希望链接位于控制器内,如上面的代码所示。

Problem was you didn't implement your controller as below. 问题是您没有实现以下控制器。

//DEFINE THE CONTROLLER
////////////////////////////////
app.controller("myController", function(){

});

Look at the following example and it's working perfectly. 看下面的例子,它运行良好。 Also I modified templateUrl to template just for demonstrate purposes. 此外,我修改templateUrltemplate只是为了演示的目的。

 <!DOCTYPE html> <html> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.js"></script> <body ng-app="myApp"> <div ng-controller="myController"> <a href="#!red">Red</a> <a href="#!green">Green</a> <div ng-view></div> </div> <script> var app = angular.module("myApp", ["ngRoute"]); //DEFINE THE CONTROLLER //////////////////////////////// app.controller("myController", function() { }); app.config(function($routeProvider) { $routeProvider .when("/", { template: "<h1>Home</h1>" }) .when("/red", { template: "<h1>Red</h1>" }) .when("/green", { template: "<h1>Green</h1>" }) .when("/blue", { template: "<h1>Blue</h1>" }); }); </script> </body> </html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HTML页面中页面加载的不同内容 - Different content on page load in an HTML page 在页面加载时使用不同的内容触发相同的模式 - Fire the same modal with different content at page load 是否可以在两个不同的 html 页面中刷新相同的内容? - is it possible to refresh the same content in two different html pages? 将两个不同的html页面内容复制到一页 - Copy two different html pages content into one page 以相同的方式在同一页面的两个不同的div中加载ajax内容 - load ajax content in two different div on the same page at the same way 在 html 加载时缩放文本并在同一页面的不同位置淡出 - Zoom text on html load and fade out at different location on same page 如何将html页面中的div内容加载到主页上的div中? - How can I load div content from html pages into a div on main page? 如何在不同的HTML页面上或刷新页面时如何继续播放HTML中的相同音乐 - How to continue playing the same music in HTML even on different HTML pages or when you refresh the page 是否可以在wordpress的不同页面上显示相同的内容 - Is it possible to display same content on different pages in wordpress Ionic-对具有不同内容的多个页面重复使用相同的HTML模板 - Ionic - Re-use the same HTML template for multiple pages with different content
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM