2016-02-04 68 views
0

這可能很簡單,但我似乎無法成功地獲得我想要的邏輯。在頁面加載ng類調用函數 - AngularJS

<div class="group-title" ng-class="{'group-hasError': !isValid(orderItem)}"> 

正如你可以看到我添加類group-hasError如果函數isValid(orderItem)返回false。

現在的問題是,這是在頁面加載時調用的。但我不想在頁面加載時調用此函數,而是在調用提交按鈕時調用

<button id="add_modified_item" ng-click="isValid(orderItem)" class="btn btn-primary btn-fixed-medium pull-right"> 

我該如何實現這一目標?

這是功能;

$scope.isValid = function(orderItem) { 
    var count = 0; 
    //By default make it true 
    var IsAllSelected = true; 
    angular.forEach(orderItem.menu_modifier_groups, function(group) { 
    var count = 0; 
    angular.forEach(group.menu_modifier_items, function(item) { 
     count += item.selected ? 1 : 0; 
    }); 
    if (count == group.max_selection_points) { 
     IsAllSelected = true; 
    } else { 
     //if one item failed All select do return false 
     IsAllSelected = false; 
    } 
    }); 
    return IsAllSelected; 
} 

任何意見讚賞

+0

你可以綁定模型值'$ scope.setValidClass = false'默認並在'isValid'上設置模型值 –

+0

@ParthTrivedi在你給我的代碼中? –

+0

它是否在窗體中?您的按鈕不是提交按鈕 –

回答

1

Defalut設置

$scope.setValidClass=false; 

查看

<div class="group-title" ng-class="(setValidClass)? 'group-hasError':'')}"> 

集模型與

//if IsAllSelected=false then set IsAllSelected to true 
$scope.setValidClass=!IsAllSelected; 
return IsAllSelected; 
+0

檢查我的答案。 –

+0

它有幫助嗎?我已更新@ moh.ABK –

+0

完美的兄弟...欣賞它! –

相關問題