对ng类应用不同的表达式
applying different expression for ng-class
嗨,我试图改变一个按钮的颜色基于这个变量的值:$scope.order.orderwindow.invoicedata。
我改变了ng-class中的表达式来做到这一点。然而,现在它没有改变,尽管我的手表功能。
directive.js
.directive('invoicetypewatch', function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
// change styling of button depending on if id is null or not
scope.$watch('order.orderwindow.invoicedata', function() {
if(scope.order.orderwindow.invoicedata.id!=null) {
scope.invoice_delete_type_button_styling={"btn btn-danger btn-block":true};
}else{
scope.invoice_delete_type_button_styling={"btn btn-danger btn-block":false};
}
});
}
};
})
view.html
<div class="col-lg-4" invoicetypewatch>
<button ng-class="{{invoice_delete_type_button_styling}}" ng-click="delete(invoice_delete_type_button,order.orderwindow.invoicedata.id)">{{invoice_delete_type_button}}</button>
</div>
我认为你只需要在指令中分配类-不需要映射对象:
if(scope.order.orderwindow.invoicedata.id!=null) {
scope.invoice_delete_type_button_styling="btn btn-danger btn-block";
}else{
scope.invoice_delete_type_button_styling="btn btn-success";
}
然后在ng-class中使用该作用域变量而不使用{{}},因为ng-class会计算
中传递的任何内容。 <button ng-class="invoice_delete_type_button_styling"
对于ng-class不需要{{}},因为它已经接受一个表达式或字符串。如果你只使用class,你会使用{{}},它会被求值
<button class="{{invoice_delete_type_button_styling}}"
我无法测试代码,因为我不知道order.orderwindow.invoicedata
是如何改变的,但我的建议是完全放弃watch
并将视图更改为:
<div class="col-lg-4" invoicetypewatch>
<button ng-class="{'btn btn-danger btn-block':(scope.order.orderwindow.invoicedata.id!=null)}"
ng-click="delete(invoice_delete_type_button,order.orderwindow.invoicedata.id)"> {{invoice_delete_type_button}}</button>
</div>
这可以工作,但它仍然取决于order.orderwindow.invoicedata
如何变化。确保这个修改发生在你的angular应用中,并开始一个新的摘要循环。
相关文章:
- ng类应用于页面加载,表达式不真实
- 如何基于正则表达式在 O365 邮件应用程序中加载不同的页面
- 在两个单词之间应用正则表达式
- 在视图模板中执行角度表达式会降低角度应用的性能
- mongo-aggregation:在$project上应用正则表达式分组、字符串处理
- 使用 css 类和正则表达式将样式动态应用于文本的各个部分
- 如何在文本框上应用正则表达式
- 正则表达式替换而不是替换(谷歌应用脚本)
- 谷歌邮件应用程序脚本中的正则表达式困难 - 在两个单词之间选择单词
- 应用不区分大小写的正则表达式后,将大写字符转换回大写
- 如何仅对字符串的一部分应用正则表达式
- 应用ng类来计算P标记内的不同表达式
- 在excel列上应用正则表达式后创建逗号分隔的文本
- 如何在元素上应用多个正则表达式
- 如何在带圆括号的子字符串匹配上应用正则表达式
- 在winrt/xaml/c#应用程序中动态评估javascript表达式
- 是否有可能在本地化的web应用程序环境中使用正则表达式进行验证?
- 如何在客户端使用jquery将正则表达式应用于文件的内容
- 为什么表达式new Date(). gettime()中的运算符严格按照从左到右的顺序应用?
- 如何使用jQuery将正则表达式应用于输入