我从a.html(在其他可信主机中)文件中加载了一个html内容:
<div>
<div><span ng-bind="propertyA"></span></div>
<div><span ng-bind="propertyB || 'Test ng-bind.'"></span></div>
<div><span ng-bind="propertyC"></span></div>
<input type="text" value="" ng-model="propertyC" />
</div>
到变量(loadedHtml)并将其绑定到我自己的页面然后编译它:
$scope.htmlContent = $sce.trustAsHtml(loadedHtml);
var elem = angular.element(".need-compile-html > *");
$compile(elem)($scope);
它正常显示,指令(ng-bind,ng-model)正常工作。
现在,我想为b.html加载一个控制器(a.js):
a.js:
(function(angular) {
angular.module("app").controller("myCtrl", [
'$scope', function($scope) {
$scope.propertyA = "Default property."
}]);
console.log("myCtrl loaded.");
})(angular);
b.html:
<div ng-controller="myCtrl">
<div><span ng-bind="propertyA"></span></div>
<div><span ng-bind="propertyB || 'Test ng-bind.'"></span></div>
<div><span ng-bind="propertyC"></span></div>
<input type="text" value="" ng-model="propertyC" />
</div>
本地js代码:
// jscontent is the content of a.js
eval(jscontent);
$scope.htmlContent = $sce.trustAsHtml(loadedHtml);
var elem = angular.element(".need-compile-html > *");
$compile(elem)($scope);
发生错误:参数&#39; myCtrl&#39;不是一个功能,未定义。 看来控制器&#39; myCtrl&#39;在$ compile(elem)之前没有注册($ scope); ,但消息&#39; myCtrl已加载。&#39;通常在控制台输出?
加载&#39; myCtrl&#39;的正确方法是什么?动态?