我是新来的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