您不必使用ng-view
和路线为您简单的情况下(其他应用程序内部部件样的角度应用程序)。您可以改用ng-include
。这是一个应用程序的例子。我更喜欢这种方法,因为它不需要使用URL位置散列等共享资源,这可能已被传统应用程序或其他小部件所使用。下面开关视图应用动态地加载不同的数据为每个视图和影响它的显示选项(显示的项目的数目):
HTML
<!DOCTYPE html>
<html>
<head>
<script data-require="[email protected]*" data-semver="2.0.3" src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script data-require="[email protected]*" data-semver="1.2.11" src="http://code.angularjs.org/1.2.11/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body>
<h1>Here is my legacy app markup</h1>
<div ng-app="app" ng-controller="appController">
<div>
<input placeholder="Number of items" ng-model="numberOfItems"/><br/>
<select placeholder="View" ng-model="currentView" ng-options="view.name for view in views"></select>
</div>
<div ng-include="currentView.url"></div>
</div>
<div id="jqueryContainer">And here is some markup generated with jQuery<br /></div>
</body>
</html>
的JavaScript
angular.module('app', []).
controller('appController', function($scope, $http) {
$scope.views = [{
name: 'view1',
url: 'view1.html',
dataUrl: 'data1.json'
}, {
name: 'view2',
url: 'view2.html',
dataUrl: 'data2.json'
}];
$scope.numberOfItems = 2;
$scope.currentView = $scope.views[0];
$scope.$watch('currentView', function(currentView) {
if(currentView && currentView.dataUrl) {
$http.get(currentView.dataUrl).success(function(data) {
$scope.data = data;
});
}
});
});
$(function(){
$('#jqueryContainer').append('<span>Some markup generated dynamically.</span>');
});
厂景.html
<div>
<h2>View1 specific markup {{data.length}}</h2>
<ul>
<li ng-repeat="item in data | limitTo:numberOfItems">{{item.text}}</li>
</ul>
</div>
data1.json
[{"text":"Text1"},{"text":"Text2"},{"text":"Text3"},{"text":"Text4"},{"text":"Text5"}]
Plunker:http://plnkr.co/edit/Y5IZmPbrrO63YrL8uCkc?p=preview
您还可以找到AngularJS文档中这种方法的有用的例子:http://docs.angularjs.org/api/ng.directive:ngInclude
您可以确定NG-应用要使用Angular。所以你的ng-app指令页面不会是Angular应用程序。 – KoIIIeY