AngularJS:动态加载从数据库中检索的HTML标记

时间:2014-11-25 09:04:09

标签: json angularjs factory ngroute

我使用factory

从JSON文件中获取数据
appService.factory('svr', ['$resource', function($resource) {
    return $resource('data/:pageName.json', {}, {query:{method:'GET', isArray:true}});
}]);

并在controller中访问它:

appController.controller('requirementCtrl', ['$scope', 'svr', function($scope, svr){
    $scope.ques = svr.query({pageName:'question'});
}]);

JSON文件中的数据包含标签,四个选项和类似值的复选框。我必须使用这些数据将其格式化为HTML标记。

我发现一种方法是在controller中创建HTML标记,然后使用div将其绑定到ng-bind-html。指令不起作用,因为我实施了ngRoute

最好的方法是什么?

1 个答案:

答案 0 :(得分:0)

我认为解决方案是"编译"你的元素如:

$compile(element.contents())(scope);
  

http://onehungrymind.com/angularjs-dynamic-templates/

其他解决方案(Ben Nadel):http://www.bennadel.com/blog/2449-directive-link-observe-and-watch-functions-execute-inside-an-angularjs-context.htm