我是 Angular 的新手,但我已经用谷歌搜索了几个小时,似乎无法弄清楚我做错了什么......我要解决的问题是能够在部分完成后执行一些 jQuery已经加载。我发现了这篇文章:http ://blog.ruedaminute.com/2012/02/angular-js-applying-jquery-to-a-loaded-partial/ ,它解释了如何使用 ng:include 而不是 ng:view,但它不起作用 - 没有包含模板,更不用说正在执行的 onload 了。
索引.html:
<!DOCTYPE html>
<html ng-app="shorelinerealtors">
<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="/lib/angular.js"></script>
<script src="/lib/angular-resource.js"></script>
<script src="/js/app.js"></script>
<script src="/js/controllers.js"></script>
<link rel="stylesheet" href="/css/style.css">
<title>Shoreline Realtors</title>
</head>
<body>
<!--It works fine using ng:view, but of course no onload option-->
<!-- <ng:view></ng:view> -->
<!--This apparently does nothing... no hello world, no alert -->
<ng:include src="$service('$route').current.template" scope="$service('$route').current.scope" onload="init()"></ng:include>
</body>
</html>
住宅.html
<h2>{{hello}}</h2>
控制器.js
function ResidentialListCtrl($scope) {
$scope.hello = "Hello World!";
this.init = function() {
alert("hello");
};
}
应用程序.js
angular.module('shorelinerealtors', ['listingsServices']).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/listings/residential', {templateUrl: '/partials/residential.html', controller: ResidentialListCtrl}).
otherwise({redirectTo: '/listings/residential'});
}]);
更新:我为此创建了一个 jsfiddle:http: //jsfiddle.net/xSyZX/7/。它适用于 ngView,但不适用于 ngInclude。我需要做些什么来在全局范围内定义 $service 吗?