【发布时间】:2015-10-19 04:04:23
【问题描述】:
鉴于我有以下两个指令。
1:Angular UI 选择 - 该指令使用隔离范围。
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>
我的问题是,如果多个指令不能在 1 个元素上一起使用,我如何从我的自定义指令中访问 ngmodel 值,是否有解决方法仍然可以保持绑定?
更新
如果我不使用空的scope: {},,我将无法在我的指令的以下函数中访问所需的 ng models 值
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();
});
}
};
}
]);
【问题讨论】:
标签: angularjs angularjs-scope angular-directive