我正在动态添加 Angular 指令内容,但无法在其中添加诸如 $scope
和 Controller
之类的功能。我该如何解决这个问题?例如:
a.html
<div ng-controller="actrl">{{aname}}</div>
b.html
<div ng-controller="bctrl">{{bname}}</div>
假设我有一个带有模板 URL 的指令:/a.html
并且
我动态地将其更改为 /b.html
,然后对于 b.html
, Angular 功能 (bctrl
) 不起作用。
jQuery
jQuery.ajax({
url: menu.templateUrl,
success: function(response) {
jQuery("view-partial").html(response);
}
});
最佳答案
使用 ng-view 包含这样的内容
$routeProvider
.when('#/a.html', {
templateUrl: 'b.html',
controller: 'bctrl',
});
这是您问题的解决方案 检查链接 http://jsbin.com/voyeki/2/edit
我遵守了你的代码
.controller("myCtrl",function($scope,$compile){
angular.element("view-partial").html($compile(response)($scope));
});
希望这能解决您的问题:-)
关于javascript - 动态添加指令时, Angular 代码无法在指令内工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30073718/