0

我目前正在使用 Angularjs(我的第一个)构建一个网站。我遇到了一个我似乎无法解决的问题。

情况:在一个表单中,某个用户必须选择几个部门(它们链接到我模型上的不同字段)。由于这些“选择器”的部门总是相同的,我选择使用指令。

该指令

app.directive('selectDepartment', function () {
    return {
        restrict: 'E',
        scope: {
            ModelfieldToUpdate: '=',
            collectionvalues: '=',
        },
        link: function (scope, elem, attrs)
        {
            //Get Attributes
            attrs.$observe('modalid', function (value) {
                scope.myModalId = value;
            });
            scope.$watch('modelfield', function (value) {
                scope.selectedValue = scope.ModelfieldToUpdate;
            });
        },
        template:'<div>' +
            '<div id="{{myModalId}}" class="modal">' +
                '<div class="modalContentCollection">' +
                    '<form action="#">' +
                        '<p ng-repeat="itm in collectionvalues>' +
                            '<input name="{{myModalId}}collectionGroup" ng-model="$parent.selectedValue" value={{itm}} type="radio" id="{{itm}}"/>' +
                            '<label for="{{itm}}">{{itm}}</label>' +
                        '</p>' +
                    '</form>' +
                '</div>' +
                '<div class="modalMenu centered">' +
                          //Some buttons here
                '</div>' +
            '</div>' +
          '</div>' +
          '</div>'
    }
});

html中的用法

<selectDepartmentmodelfield="Unit.DepartmentA" collectionvalues="MyDepartments" modalid="modalSelectDepartmentA"></selectafdeling>
<selectDepartmentmodelfield="Unit.DepartmentB" collectionvalues="MyDepartments" modalid="modalSelectDepartmentB"></selectafdeling>

输出 html(用于 MyDepartments 中的第一个项目)

Snippet out of first:
    <input name="modalSelectDepartmentAcollectionGroup" ng-model="$parent.selectedValue" value=FIN type="radio" id="FIN"/>
==> Does work

Snippet out of the second:
    <input name="modalSelectDepartmentBcollectionGroup" ng-model="$parent.selectedValue" value=FIN type="radio" id="FIN"/>
==> Does not work. Even clicking it won't select it.

问题:

尽管单选按钮组有不同的名称,但它们似乎仍充当同一个单选按钮组。有人知道为什么吗?

4

0 回答 0