簡體   English   中英

帶有指令的Angular ngModel

[英]Angular ngModel with directive

我試圖將一個對象傳遞給一個指令,然后可以更新這個值。 到目前為止我有以下內容:

 <competence-select ng-model="module.selectedCompetence"></competence-select>

指示

angular.module('Competence').directive("competenceSelect", ['competenceService', function (competenceService) {
    return {
        restrict: "E",
        templateUrl: 'js/modules/Competence/directives/competence-select/competence-select.html',
        require: 'ngModel',
        link: function (scope, element, attr, ngModel) {
            ngModel.$setViewValue = scope.competenceList;
            scope.competences = [];


   competenceService.getCompetenceList().then(function (result) {
                scope.competences = result;
            })
        }
    };
}]);

(注意要求)

然后我的指令html:

    <label translate="FORMS.COMPETENCES"></label>
<ui-select multiple reset-search-input="true" ng-model="competenceList" theme="bootstrap"
           ng-disabled="disabled">
    <ui-select-match placeholder="{{ 'FORMS.COMPETENCES_PLACEHOLDER' | translate }}">{{$item.name}}
        &lt;{{$item.competence_type_id == 1 ? 'Faglig' : 'Personlig' }}&gt;</ui-select-match>
    <ui-select-choices group-by="someGroupFn"
                       repeat="competence in competences | propsFilter: {name: $select.search, competence_type_id: $select.search}">
        <div ng-bind-html="competence.name | highlight: $select.search"></div>
        <small>
            {{competence.name}}
            {{ 'COMPETENCES.TYPE' | translate:{TYPE: competence.competence_type_id} }}
        </small>
    </ui-select-choices>
</ui-select>

現在我想做的事情很簡單:

ngModel$setViewValue = scope.competence;

設置這個應該設置ng-model視圖是NG-模型我的指令集。 從而將變量“up”解析為聲明指令:

<competence-select ng-model="module.selectedCompetence"></competence-select>

可悲的是,事實並非如此。

誰能告訴我我做錯了什么?

在指令中進行這些更改

angular.module('Competence').directive("competenceSelect", ['competenceService', function (competenceService) {
    return {
        restrict: "E",
        templateUrl: 'js/modules/Competence/directives/competence-select/competence-select.html',
        require: 'ngModel',
        scope:{ ngModel : "=" }, 
        link: function (scope, element, attr, ngModel) {
            ngModel.$setViewValue = scope.competenceList;
            scope.competences = [];


   competenceService.getCompetenceList().then(function (result) {
                scope.competences = result;
                scope.ngModel = scope.competences;
            })
        }
    };
}]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM