Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.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
AngularJS数据绑定_Angularjs_Ng Class - Fatal编程技术网

AngularJS数据绑定

AngularJS数据绑定,angularjs,ng-class,Angularjs,Ng Class,我有两个很好的代码示例,但我不理解其中的区别。 因此,第一个是: AngularJS: $scope.state = {presentation: true}; <div ng-class="{{state.presentation}} ? 'on' : 'off'"></div> $scope.presentation = true; <div ng-class="presentation ? 'on' : 'off'"></div> H

我有两个很好的代码示例,但我不理解其中的区别。 因此,第一个是:

AngularJS:

$scope.state = {presentation: true};
<div ng-class="{{state.presentation}} ? 'on' : 'off'"></div>
$scope.presentation = true;
<div ng-class="presentation ? 'on' : 'off'"></div>
HTML:

$scope.state = {presentation: true};
<div ng-class="{{state.presentation}} ? 'on' : 'off'"></div>
$scope.presentation = true;
<div ng-class="presentation ? 'on' : 'off'"></div>
HTML:

$scope.state = {presentation: true};
<div ng-class="{{state.presentation}} ? 'on' : 'off'"></div>
$scope.presentation = true;
<div ng-class="presentation ? 'on' : 'off'"></div>

为什么我不能在第二个示例中使用类似的内容:

<div ng-class="{{presentation}} ? 'on' : 'off'"></div>

当我将{{}与
$scope.presentation=true一起使用时它在ng类中不起作用,但我可以像文本一样使用{{presentation},我的意思是
{{presentation}}
,而且效果很好。为什么?

但是使用
$scope.state={presentation:true}我甚至可以在ng类中使用{{presentation},效果很好


有什么区别

您不应该在ng属性中使用这样的表达式。并且它将失败(无状态对象或无状态对象):

错误:[$parse:syntax]语法错误:标记“?”不是主标记 表达式[?'on':'off']的第2列处的表达式,从处开始 [?'开':'关']

这是正常的,因为angularJs将其作为angularJs执行,所以当您键入时:

<div ng-class="presentation ? 'on' : 'off'"></div>

在内部,angularJS将其作为表达式执行,也可以这样编写:

<div class="{{presentation ? 'on' : 'off'}}"></div>


可能是由于写入/读取到不同的范围而导致的。你能把你的代码放在一个plunker里吗?
div>{{presentation}
在你的第二个例子中是这样的吗?我已经尝试了你所期望的。它工作得很好。这是普朗克。另外,您不需要插值,您可以将其用作普通JavaScript对象或使用原语类型。由于$scope.presentation和$scope.state={presentation:true},非课堂教学不起作用;设置为true。如果您将其中任何一个设置为false,则会显示红色,表示已应用“关闭”类。谢谢。我理解我的错误。