【发布时间】:2014-02-17 15:43:36
【问题描述】:
我有一个指令
app.directive("dir", function($compile, $sce){
return{
restrict: "E",
link: function(scope, element, attr){
scope.$watch('content',function(){
var html = $sce.trustAsHtml(attr.content);
scope.alabala = $compile(html)(scope);
},true);
},
template: "<div ng-bind-html='alabala'></div>",
}
});
控制器:
function MainController($scope, $http, customService, $location, $sce, $compile){
$scope.init = function(){
customService.get().success(function(data) {
var html = $sce.trustAsHtml(data);
$("#dir").attr("content", data);
});
};
}
在我的索引页面上我有:
<div id="div" ng-controller="MainController" class="pull-right span3" ng-init="init()">
<dir id="dir" ></dir>
</div>
我的自定义服务每次返回一个不同的 html 包含例如
<button ng-click='click()'>Click me</button>
我想要做的是每次我在指令的内容中推送不同的值以编译它并将其放入我的 html 并从我的控制器处理单击功能。因为我是 AngularJS 的新手,所以我一直在努力解决这个问题。请帮忙。
【问题讨论】:
标签: javascript jquery html angularjs angularjs-directive