I'm trying to do something similar to this fiddle.
What I expect to happen is that a select list is displayed and when one of the options is selected the word true
or false
is displayed on this line of code:
<span data-ng-bind="cardType.transactionFee"></span>
The $watch
part of it is working as the alert that says hey, cardType has changed!
is displayed whenever the card type changes.
How can I pass the changed values to the directive
?
.controller('AddCreditCardChargeCtrl', ['$scope', function($scope) {
$scope.creditCards = [
{ name: 'VISA DEBIT/DELTA', value: 'DEL', transactionFee: false},
{ name: 'VISA CREDIT', value: 'VIS', transactionFee: true },
{ name: 'MASTERCARD CREDIT', value: 'MSC', transactionFee: true },
{ name: 'MASTERCARD DEBIT', value: 'MCD', transactionFee: false },
{ name: 'MAESTRO', value: 'MAE', transactionFee: false },
{ name: 'SWITCH', value: 'SWI', transactionFee: false },
{ name: 'VISA ELECTRON', value: 'ELC', transactionFee: false },
{ name: 'SOLO', value: 'SOL', transactionFee: false }
];
$scope.selectAction = function() {
alert($scope.creditCardModel);
alert(hasTransFee($scope.creditCardModel));
$scope.cardType=hasTransFee($scope.creditCardModel);
// console.log(cardType.name)
// console.log(cardType.transactionFee)
console.log('watch')
$scope.$watch("cardType.transactionFee", function() {
alert('hey, cardType has changed!');
});
}
var hasTransFee = function(cardType)
{
for (var i=0; i < $scope.creditCards.length; i++) {
if($scope.creditCards[i].value==cardType && $scope.creditCards[i].transactionFee == true){
return true;
}
}
return false;
}
}])
.directive('bhtDiscountPrice',['BasketFactory',
function(BasketFactory){
return{
restrict:'EA',
scope:true,
link:function(scope,elem,attrs){},
template:'<span data-ng-bind="cardType.transactionFee"></span>'
}
}])