如何在动态html内容中添加否定运算符
'<div class="col-lg-4 col-md-4 col-sm-4 col-xs-12 form-group1" ng-class="{\'has-error\': (form.Year.$error.required && form.Year.$touched), \'has-success\': !(form.Year.$error.required)}"><select ng-model="year" class="form-control" ' + strAttrs[2] + ' ng-options="year.value as year.name disable when year.disabled for year in years" class="form-control" ng-required="true" name="Year"></select></div>'
在上面的代码中,我给出了这样的否定运算符,
!(form.Year。$ error.required)
但是每次它都为null,我想我还没有在动态html内容中添加逻辑运算符。
任何帮助都很明显,在此先感谢。 :)
答案 0 :(得分:0)
以下适用于我:
<div ng-class="{'ok': (success && !error), 'not-ok': (!success || error)}">Result Box</div>
请注意,我不需要转义单引号。当我尝试转义单引号时,它不起作用。
我还注意到你的选择中出现了一些无效的引用:
class="form-control" ' + strAttrs[2] + ' ng-options="year.value as year.name disable when year.disabled for year in years"
你也可以两次定义类,但这应该是静默失败。
这是一个很小的工作示例:
<!doctype html>
<html ng-app="myApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.js"></script>
<script>
angular.module('myApp', [])
.controller('MyController', ['$scope', function ($scope) {
$scope.success = true;
$scope.error = false;
}]);
</script>
<style>
.not-ok { border: 1px solid red; }
.ok { background-color: green; }
</style>
</head>
<body ng-controller="MyController">
<input type="checkbox" ng-model="success">Success {{success}}
<br/>
<input type="checkbox" ng-model="error">Error {{error}}
<div ng-class="{'ok': (success && !error), 'not-ok': (!success || error)}">Result Box</div>
</body>
</html>
&#13;