javascript - AngularJs 指令 : nested ng-class is failing

标签 javascript angularjs angularjs-directive ng-class

当我需要嵌套的 ng 类时,我遇到了 AngularJs 指令的一个奇怪问题。 Here是一个 JSFiddle,它显示了假设情况下的问题。

HTML

<div in-directive ng-class="{ 'testing1': 1 == 1 }"></div>

Javascript

var myApp = angular.module('myApp', []);

myApp.directive('inDirective', function () {
    return {
        restrict: 'A',
        replace: true,
        template: '<div ng-class="{ \'testing\': 1 == 1 }">Hi</div>'
    };
});

AngularJS 错误

语法错误:从 [{ 'testing': 1 == 1 }].

[{ 'testing11': 1 == 1 } { 'testing': 1 == 1 }]

这是一个无效的 JSON。我在这里做错了什么吗?目前,我所做的解决方法是将 ng-class 移动到上层 div 元素。但这显然不是我们想要的。

最佳答案

此类似问题已在此处报告 #8159 Directive with 'replace: true' and template .

并且 AngularJS 团队决定在使用 template/templateUrlreplace 标志设置为 true 的指令时不支持复杂的属性合并。

这个问题是他们 are deprecating 的原因之一指令的 replace 标志。

你也可以在这里看到很长的讨论eec6394: deprecate replace directives

关于javascript - AngularJs 指令 : nested ng-class is failing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25389955/

相关文章:

javascript - 在 Angular 1.x 中当指令中有 "watching"时;为什么观察返回变量的函数与仅仅观察变量的行为不同?

javascript - 输入框的值在 4 个数字后重置

javascript - 从 Node.JS/Javascript 页面的标签栏中删除图像

javascript - Angular js : resolve dependency route

javascript - 使用javascript更改任意文本的背景颜色

javascript - 如何使用 underscore.js 找到 javascript 数组中存在的给定值?

javascript - AngularJS 在屏幕上显示两个 View /模板

angularjs - 检测是否通过 SSL 访问应用程序?

javascript - angularjs FileReader 返回未定义

javascript - 如何使用 Angular 表达式值作为 html 元素的属性