2014-09-03 96 views
2

我使用重量輕的jQuery與AngularJS(這是從源說,我發現的溶液)走來添加屬性到一個刪除按鈕,如下所示:在AngularJS有條件呼叫功能

if (item.CustomerGuid == item.OwnerCustomerGuid) { 
    $scope.isDeleteDisabled = false; 
    $('#deleteFreight').attr("ng-click", "FreightGroup_delete(" + item.ListGuid + ", " + item.CustomerGuid + ")"); 
} else { 
    $scope.isDeleteDisabled = true; 
} 

當我檢查變化得到反映,但是當我嘗試點擊按鈕時,所需的功能沒有被調用。

需要幫助。

.cshtml(前):

<button class="btn btn-danger" ng-disabled="isDeleteDisabled" 
id="deleteFreight">Delete Freight Group </button> 

(後):

<button class="btn btn-danger active" ng-disabled="isDeleteDisabled" 
ng-click="FreightGroup_delete(7338bb3a-9e6b-49d7-be08-7b60ff47ba61, 
    ce3adfcc-d8a1-4a6e-a2b7-d15dc1980384)" id="deleteFreight"> 
Delete Freight Group </button> 

回答

1

請勿將jquery與angularjs混合使用。如果我正確理解你想達到的目標,這應該是正確的做法。

JS:

$scope.item = item; 

HTML:

<button 
    class="btn btn-danger" 
    ng-click="FreightGroup_delete(item.ListGuid, item.CustomerGuid)" 
    ng-disabled="item.CustomerGuid !== item.OwnerCustomerGuid" 
    id="deleteFreight">Delete Freight Group 
</button> 
+0

感謝您的建議,它很簡單。 – RandomUser 2014-09-03 10:27:18

1

使用jQuery功能不會影響範圍。 請與下面的代碼

if (item.CustomerGuid == item.OwnerCustomerGuid) { 
    $scope.isDeleteDisabled = false; 
    $scope.dynamicAttr= [ 
     { attr: 'ng-click', value: "FreightGroup_delete(" + item.ListGuid + ", " + item.CustomerGuid + ")" } 
    ]; 
} else { 
    $scope.isDeleteDisabled = true; 
} 

和你的HTML的外觀的檢查這樣

<button class="btn btn-danger" ng-disabled="isDeleteDisabled" 
id="deleteFreight" dyn-attrs="dynamicAttr">Delete Freight Group </button> 

讓我知道它是否正在工作。

+0

加入'$ scope.apply()之後;'當我打電話,我改變刪除按鈕的特性功能我得到這個錯誤。 'Scope。$ scope.getFreights' – RandomUser 2014-09-03 10:26:36

+0

請檢查我的更新回答 – sameer 2014-09-03 10:37:39