鑑於我有以下兩個指令。1個元素上的多個指令
1:Angular UI select - 此指令使用隔離範圍。
2:myDirective - 在我的自定義指令,我也用隔離範圍訪問ngModel
我收到多個指令錯誤的值不能共享隔離範圍。這就是我在我的指令中聲明隔離範圍的方式。
require: 'ngModel',
scope: {
modelValue: "=ngModel"
},
link: function (scope, el, attrs, ctrl) {
我用它像:
<ui-select myDirective multiple ng-model="GroupsModel" theme="select2" ng-disabled="disabled" style="width: 300px;" hidden-text-box ">
<ui-select-match placeholder="groups">{{$item}}</ui-select-match>
<ui-select-choices repeat="color in Groups ">
{{color}}
</ui-select-choices>
</ui-select>
我的問題是,我怎麼能訪問從我的自定義指令的ngmodel值,如果多個指令不能一起1元所使用的,是有圍繞這一點的工作仍將保持約束力?
更新
我不能在我的指令下列功能訪問所需的NG模型價值,如果我不使用空scope: {},
scope.reset = function() {
var modelValue =ctrl.$viewValue;
$timeout(function() {
el[0].focus();
}, 0, false);
};
這裏是我的指令:
var app = angular.module('app');
app.directive('resetField', [
'$compile', '$timeout', '$http', function ($compile, $timeout, $http) {
return {
require: 'ngModel',
link: function (scope, el, attrs, ctrl) {
// compiled reset icon template
var template = $compile('<i ng-show="enabled" ng-mousedown="reset()" class="fa fa-floppy-o" style="padding-left:5px"></i>')(scope);
el.after(template);
scope.reset = function() {
var modelValue =ctrl.$viewValue;
$timeout(function() {
el[0].focus();
}, 0, false);
};
el.bind('input', function() {
scope.enabled = !ctrl.$isEmpty(el.val());
})
.bind('focus', function() {
scope.enabled = !ctrl.$isEmpty(el.val());
scope.$apply();
})
.bind('blur', function() {
scope.enabled = false;
scope.$apply();
});
}
};
}
]);
我用我的指令更新了我的問題,你可以看看 – joe