Javascript 使用ng bind HTML的HTML集中的角度绑定
我的基本用例是这样的-当一个特定的路由被点击时(Javascript 使用ng bind HTML的HTML集中的角度绑定,javascript,angularjs,Javascript,Angularjs,我的基本用例是这样的-当一个特定的路由被点击时(#/abc),我需要对服务器进行POST调用,并呈现作为响应发送的HTML。由于HTML依赖于服务器调用,我不喜欢将此逻辑写入$routeProvider.when 我的解决方案- 开辟路线 在ng视图中加载空白视图(用于应用程序的其他部分) 空白视图包含一个带有 在控制器中,创建一个$http.post,返回数据后,我将上面的表达式设置为$scope.responseHtml=$sce.trustAsHtml(数据) 这工作得非常好,但是,我
#/abc
),我需要对服务器进行POST调用,并呈现作为响应发送的HTML。由于HTML依赖于服务器调用,我不喜欢将此逻辑写入$routeProvider.when
我的解决方案-
- 开辟路线
- 在ng视图中加载空白视图(用于应用程序的其他部分)
- 空白视图包含一个带有
- 在控制器中,创建一个
,返回数据后,我将上面的表达式设置为$http.post
$scope.responseHtml=$sce.trustAsHtml(数据)代码>
$compile
它
app.directive("ngCompile", function($compile){
return {
scope: {
"ngCompile": "=",
"ngCompileScope": "="
},
link: function($scope, $element){
$scope.compile = function(){
$element.html($scope.ngCompile);
$compile($element.contents())($scope.ngCompileScope);
}
$scope.$watch("ngCompile",function(){
$scope.compile();
});
}
}
});
或没有单独的范围:
app.directive('ngCompile', function ($compile) {
return {
restrict: 'A',
replace: true,
link: function (scope, ele, attrs) {
scope.$watch(attrs.ngCompile, function(html) {
ele.html(html);
$compile(ele.contents())(scope);
});
}
};
});