我是新來的angularjs和工作指令下拉,但有問題選擇初始值。我必須通過選項的Id來選擇作爲指令的屬性。我將屬性值設置爲一個範圍變量,然後在ng-Init中使用。使用angularjs中的指令在下拉菜單中選擇一個選項
這裏是我的指令代碼:
export class ChoiceGroup
{
constructor()
{
var directive: ng.IDirective = {};
directive.restrict = "E";
directive.replace = true;
directive.template = '<div><select name="cmbCG" ng-model="dataSel" ng-options="c.DisplayName for c in data"></select></div>';
directive.link = function ($scope: any, element: JQuery, attributes: any) {
var injector = angular.element(document).injector();
var key = attributes.key;
var cgCacheService: ChoiceGroupsCacheService;
seCGCacheService = injector.get('seChoiceGroupsCacheService');
var values: Array<ChoiceValue>;
values = seCGCacheService.GetChoiceValues(key);
$scope.data = values;
if (attributes.selectedvalue) {
$scope.selectedvalue = values[attributes.selectedvalue];
}
}
return directive;
}
的代碼是打字稿。這裏是HTML:
<choicegroupcombo key="RecurrenceFrequency" selectedvalue="3"></choicegroupcombo>
如果我硬編碼dataSel的價值即NG-的init =「dataSel = 3」,那麼它工作正常,但是當我設置爲範圍變量,然後它不工作。那麼我該如何解決這個問題。
編輯 已解決。我相應地更新了代碼。
這裏有什麼實際的目標是什麼?你的代碼似乎沒有嘗試做任何事情,除了正常的選擇框。 – codevinsky