Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 1个元素上的多个指令_Angularjs_Angularjs Scope_Angular Directive - Fatal编程技术网

Angularjs 1个元素上的多个指令

Angularjs 1个元素上的多个指令,angularjs,angularjs-scope,angular-directive,Angularjs,Angularjs Scope,Angular Directive,鉴于我有以下两条指示 1:角度UI选择-此指令使用隔离范围 2:myDirective-在我的自定义指令中,我还使用隔离作用域来访问ngModel的值 我收到多个指令错误,无法共享隔离范围。这就是我在指令中声明隔离范围的方式 require: 'ngModel', scope: { modelValue: "=ngModel" }, link: function (sc

鉴于我有以下两条指示

1:角度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>
这是我的指示:

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();
                        });
                }
            };
        }
    ]);
var-app=angular.module('app');
应用指令('resetField'[
“$compile”、“$timeout”、“$http”、函数($compile、$timeout、$http){
返回{
要求:'ngModel',
链接:函数(范围、el、属性、ctrl){
//编译的重置图标模板
变量模板=$compile(“”)(范围);
el.之后(模板);
scope.reset=函数(){
var modelValue=ctrl.$viewValue;
$timeout(函数(){
el[0].focus();
},0,假);
};
el.bind('input',function(){
scope.enabled=!ctrl.$isEmpty(el.val());
})
.bind('focus',function(){
scope.enabled=!ctrl.$isEmpty(el.val());
作用域:$apply();
})
.bind('blur',function(){
scope.enabled=false;
作用域:$apply();
});
}
};
}
]);

如果您仅使用隔离作用域来获取select的ng模型,则可以不使用隔离作用域


在link函数中,只需使用
scope[attrs.ngModel]
,您甚至可以监视它(只要ngModel是对象属性
ng model=obj.prop1

您可以在一个元素上使用多个指令,问题是在一个元素上应用多个隔离的作用域是无效的,您可以使用在
myDirective
中要求另一个指令:

angular.directive('myDirective', [function(){

    return {
        scope: false,
        require: 'ngModel',
        link: function(scope, element, attr, ctrl){

            scope.modelValue = ctrl.$viewValue;

        }
    }

}])

在指令中使用“require”可以访问另一个指令的控制器。然后,您可以将该控制器注入到指令实现的参数中。但是,如果执行此操作,则不能使用隔离作用域

如果需要使用两个指令,请确保第二个指令不是孤立的。

很抱歉,我没有提到另一个情况。MyDirective有时在没有多个指令的情况下使用。考虑到这一点,您的解决方案有效吗?但有时,我需要在没有其他指令的情况下使用myDirective。在这种情况下,如果没有隔离作用域,如何访问ngmodel值?您将使用的作用域将是元素所在的控制器的作用域。你可以修改相应控制器中的变量。我已经用我的指令更新了我的问题。你可以看一下吗?如果你使用的是普通指令而不是单独的指令,你可以访问该指令所在的控制器范围,因此你不需要定义范围。此指令的范围是使用该指令的视图的控制器的范围。你可以直接访问GroupsModel我已经用我的指令更新了我的问题你能看一下吗
angular.directive('myDirective', [function(){

    return {
        scope: false,
        require: 'ngModel',
        link: function(scope, element, attr, ctrl){

            scope.modelValue = ctrl.$viewValue;

        }
    }

}])