在AngularJS中,可以使用ng-include
指令来只加载组件的HTML而不是整个ng-view
。下面是一个代码示例:
Component 1
This is the content of component 1.
Component 2
This is the content of component 2.
// app.js
var app = angular.module('myApp', ['ngRoute']);
app.config(function($routeProvider) {
$routeProvider
.when('/component1', {
template: ''
})
.when('/component2', {
template: ''
})
.otherwise({
redirectTo: '/component1'
});
});
app.controller('MainController', function($scope) {
// Controller logic goes here
});
在上面的示例中,ng-include
指令用于动态加载组件的HTML内容。在$routeProvider
中,我们使用template
属性来指定要加载的HTML文件,然后将其传递给ng-include
指令。这样,只有组件的HTML内容会被加载到ng-view
中,而不是整个ng-view
。