【发布时间】:2016-02-03 21:44:49
【问题描述】:
我编写了一个指令,该指令根据按下的键更新范围变量。该代码对我来说工作正常,但变量不会更新,直到焦点重新放在下拉菜单上或拉出。下面是html和指令代码:
<button type="button" drop-select ="card.purchaseType">{{card.purchaseType}}</button>
appModule.directive('dropSelect', function() {
return {
scope: {
purchaseType:'=dropSelect'
},
link: function(scope, element, attrs){
var items =["Retail Purchases","Prescription Purchases", "Retail and Prescription Purchases"];
element.on('keyup', function(e){
// Get the character pressed
var stringChar = String.fromCharCode(e.keyCode), currentItem = scope.purchaseType;
for(var itemCount = 0; itemCount < items.length; itemCount++){
if(stringChar == items[itemCount].charAt(0) &&
currentItem != items[itemCount]){
scope.purchaseType = items[itemCount];
}
}
});
}
}
})
【问题讨论】:
-
请重现您的问题,使用 plunker 进行演示。
-
我不知道为什么它不更新范围但是如果它更新了按钮内的值即使指令更新相同也不会改变。
标签: angularjs angularjs-directive angular-ui-bootstrap