Javascript 具有方向性的角模型
我试图将一个对象传递到一个指令中,然后该指令可以更新这个值。到目前为止,我有以下几点:Javascript 具有方向性的角模型,javascript,angularjs,Javascript,Angularjs,我试图将一个对象传递到一个指令中,然后该指令可以更新这个值。到目前为止,我有以下几点: <competence-select ng-model="module.selectedCompetence"></competence-select> (请注意要求) 然后是我的指令html: <label translate="FORMS.COMPETENCES"></label> <ui-select multiple reset-sea
<competence-select ng-model="module.selectedCompetence"></competence-select>
(请注意要求)
然后是我的指令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}}
<{{$item.competence_type_id == 1 ? 'Faglig' : 'Personlig' }}></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>
设置此选项应将视图中的ng模型设置为我在指令中设置的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;
})
}
};
}]);
显然,您需要调用$render()
来更新模型,而不仅仅是视图。只是在谷歌上搜索了一下,发现@floribon是我们需要调用的正确选项。$render()$setViewValue
是一个函数。@Maverick很高兴知道。请随时发布答案
<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;
})
}
};
}]);