我是angularjs的新手并且正在为下拉列表指示但是在选择初始值时遇到问题。我必须传递选项的Id以选择作为指令的属性。我在范围变量中设置属性值,然后在ng-Init中使用。
这是我的指令代码:
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;
}
代码在Typescript中。这是HTML:
<choicegroupcombo key="RecurrenceFrequency" selectedvalue="3"></choicegroupcombo>
如果我硬编码dataSel的值,即ng-init =“dataSel = 3”,那么它工作正常,但是当我设置为范围变量时,它就不起作用了。那么我该如何解决这个问题。
修改 解决了。我已相应更新了代码。
答案 0 :(得分:0)
我认为这可能是“过度工程化”的经典案例。
除了标准选择框之外,您的代码似乎没有尝试执行任何操作。
请改为尝试:
app = angular.module('app', [])
app.controller 'MainCtrl', ($scope) ->
# $scope.values = ChoiceGroupsCacheService(key)
$scope.values = [
{ id: 0, label: 'Zero' }
{ id: 1, label: 'One' }
{ id: 2, label: 'Two' }
{ id: 3, label: 'Three' }
]
$scope.number = 2
和视图:
<select ng-model="number" ng-options="item.id as item.label for item in values"></select>
代码在coffeescript中: