Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何强制从JSON传递的表达式进入角度范围?_Javascript_Angularjs_Json_Angularjs Scope - Fatal编程技术网

Javascript 如何强制从JSON传递的表达式进入角度范围?

Javascript 如何强制从JSON传递的表达式进入角度范围?,javascript,angularjs,json,angularjs-scope,Javascript,Angularjs,Json,Angularjs Scope,我正在寻找最佳实践方向,了解如何使用来自JSON的HTML并在单击时触发模式/切换 JSON有100个条目,大约10个包含将触发弹出窗口的链接。它们在文本中显示为“Schedule”(其中#是A-G) 我的第一种方法是简单地将角度标记写入JSON,并将其呈现到范围中: 在JSON中: "Are the details defined in <a href='/#/' class='schedule' data-ng-click='modal.active = modal.acti

我正在寻找最佳实践方向,了解如何使用来自JSON的HTML并在单击时触发模式/切换

JSON有100个条目,大约10个包含将触发弹出窗口的链接。它们在文本中显示为“Schedule”(其中#是A-G)

我的第一种方法是简单地将角度标记写入JSON,并将其呈现到范围中:

在JSON中:

"Are the details defined in 
<a href='/#/' class='schedule' 
   data-ng-click='modal.active = modal.active === true ? false : true'>
      Schedule G
</a>?"
”中定义了详细信息
?"
显然,这不起作用,因为angular不知道ng点击或模态$sce正在为trustAsHTML做它的工作-但是,我找不到任何东西可以识别这个表达式

我走对了吗

我一直在浏览$apply、$compile和$parse文档,但它们似乎都不喜欢我所做的


或者,是否最好为“Schedule”使用正则表达式,然后通过编程将角度表达式构建到其中?关于如何完成这项任务,我的回答是空的。

虽然我还没有完全解决这个问题,但我知道关于方向的问题的答案是可靠的$compile。我发现了它,它的灵感来自一个类似的问题。。。在帖子中引用。

HTML

<p data-ng-bind-html="__services.trustAsHTML(__data.steps[step.text])" data-angular-compile></p>

JSON对象具有带有
ng click
属性的HTML链接,使用
ng bind HTML
,使用
$sce
trustAsHtml
使HTML安全。我还使用了一个自定义的angular compile指令,在json加载到
$q
promise中之后,将angular click listener编译到应用程序中。

如果要在json中使用angular语法,则$compile是正确的路径,尽管这是一个丑陋的解决方案。相反,您应该将JSON解析为ng repeat的一部分,并根据需要在html中放置元素。你能在这里提供你的代码吗?@JamesGentes谢谢你。我将ng repeat用于整个模型,但是,这个单子对象有一个链接中间语句,需要读取现有的$scope。你能解释为什么这是一个丑陋的解决方案-除了,它感觉丑陋?确实如此。我如何以这种方式使用ng repeat?啊,那么您可能一直在使用$compile。这种方法没有错,只是不是最简单的解决方案,但您可能会陷入这种情况。
angular.module('app.directives.AngularCompile', [], ["$compileProvider", function($compileProvider) {
$compileProvider.directive('angularCompile', ["$compile", function($compile) {
    return function(scope, element, attrs) {
        scope.$watch(
            function(scope) {
                return scope.$eval(attrs.angularCompile);
            },
            function(value) {
                element.html(value);
                $compile(element.contents())(scope);
            }
        );
    };
}])
}]);