試圖根據this blog post從angular-ui-bootstrap修飾typeahead指令並且遇到一些麻煩。我需要替換綁定(直到某些PR會修復它的行爲如預期),所以我想我可以獲取指令來裝飾,調用link.apply(this,arguments),然後再插入綁定,如圖所示在此代碼示例:Angular.js修飾的指令沒有得到指令範圍
angular.module('ui.bootstrap').config(function($provide)
{
$provide.decorator('typeaheadDirective', function($delegate)
{
var directive = $delegate[0]; //get the current directive with that name;
//console.log('directive', directive) // I do get the directive here, just checking
var link = directive.link; //getting the current link function. in which we would like to replace the keybinding
//console.log('link:', link)
directive.compile = function()
{
return function(scope, element, attrs)
{
link.apply(this, arguments);
//element.unbind('keydown');
element.bind('keydown', function(evt)
{
//typeahead is open and an "interesting" key was pressed
console.log('scope matches', scope);
if (scope.matches.length === 0 || HOT_KEYS.indexOf(evt.which) === -1)
{
//@alon TODO:check this
return;
}
evt.preventDefault();
if (evt.which === 40)
{
scope.activeIdx = (scope.activeIdx + 1) % scope.matches.length;
scope.$digest();
}
else if (evt.which === 38)
{
scope.activeIdx = (scope.activeIdx ? scope.activeIdx : scope.matches.length) - 1;
scope.$digest();
}
else if (evt.which === 13 || evt.which === 9)
{
if (scope.activeIdx !== -1 && scope.activeIdx !== 0)
{
scope.$apply(function()
{
scope.select(scope.activeIdx);
});
}
else
{
evt.stopPropagation();
resetMatches();
scope.$digest();
}
}
else if (evt.which === 27)
{
evt.stopPropagation();
resetMatches();
scope.$digest();
}
});
};
};
return $delegate;
});
});
,但我得到那個指示scope.matches不存在(沒有定義)的錯誤 - 這意味着該指令原來的範圍並沒有真正運行 - 嘗試這種與其他變量從原始指令失敗並出現相同的錯誤。我該如何解決這個問題?
感謝您的幫助