Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.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 角度自定义指令双向绑定在uib模式内不起作用_Angularjs_Angularjs Directive_Angularjs Scope_Angular Ui Bootstrap - Fatal编程技术网

Angularjs 角度自定义指令双向绑定在uib模式内不起作用

Angularjs 角度自定义指令双向绑定在uib模式内不起作用,angularjs,angularjs-directive,angularjs-scope,angular-ui-bootstrap,Angularjs,Angularjs Directive,Angularjs Scope,Angular Ui Bootstrap,我在uib模式内部使用自定义指令双向绑定时遇到困难。 该指令从uib模式范围获取模型变量,但一旦更改,uib模式中的模型将不适用 Iconsole.log范围,并获得仅使用$scope.$parent.$parent访问uib模式范围的指令。。。由于某种原因,双向绑定不起作用 现在,我使用directive model属性执行eval(): link: function (scope, element, attrs, ctrls) { scope.uibScopeModel = attr

我在uib模式内部使用自定义指令双向绑定时遇到困难。 该指令从uib模式范围获取模型变量,但一旦更改,uib模式中的模型将不适用

I
console.log
范围,并获得仅使用
$scope.$parent.$parent
访问uib模式范围的指令。。。由于某种原因,双向绑定不起作用

现在,我使用directive model属性执行
eval()

link: function (scope, element, attrs, ctrls) {
    scope.uibScopeModel = attrs.ngModel;
}

eval('$scope.$parent.$parent.' + $scope.uibScopeModel + ' = $scope.model');
这是可行的,但似乎不是解决问题的好办法

为什么双向绑定不能正常工作?这是uib modal的已知问题吗?如何解决呢

我的自定义指令:

angular.module('mean.upload').directive('uploadDirective', function (Upload) {
    return {
        restrict: 'E',
        require: 'ngModel',
        scope: {
            model: '=ngModel', // the model
            type: '@type',    // type of upload (imageSingle/fileSingle)
            required: '@required', //
            fileTypeAccept: '@fileTypeAccept',
        },
        templateUrl: function (elem, attr) {
            switch (attr.type) {
                case 'imageSingle':
                    return '/upload/views/directive_templates/image-single.tpl.html';
                case 'fileSingle':
                    return '/upload/views/directive_templates/file-single.tpl.html';
            }
        },
        link: function (scope, element, attrs, ctrls) {
            scope.uibScopeModel = attrs.ngModel;
        },
        controller: function ($scope, $rootScope) {
            $scope.uploader = Upload.getDefaultUploader();
            $scope.uploader.onCompleteItem = function (item, response, status, headers) {
                if ($scope.type === 'imageSingle') {
                    $scope.model = Upload.uploadPath + response.filename;
                } else if ($scope.type === 'fileSingle') {
                    $scope.model = {
                        src: Upload.uploadPath + response.filename,
                        name: item.file.name,
                        type: item.file.type
                    };
                }
                eval('$scope.$parent.$parent.' + $scope.uibScopeModel + ' = $scope.model');
            }
        }
    }
});
标记:

<upload-directive
        ng-model="file"
        type="fileSingle"
        file-type-accept="application/pdf">
</upload-directive>

指令模板:

<div nv-file-drop uploader="uploader">
    <div class="well drop-zone" nv-file-over uploader="uploader">
        <div class="row" ng-show="model">
            <div class="uploaded-file-teaser col-sm-6 col-sm-offset-3 validation-icon-container">
                <div class="teaser-edit-buttons">
                    <button class="btn btn-xs" ng-click="model=null">
                        <i class="fa fa-trash"></i>
                    </button>
                </div>
                <div class="file-container type-{{model.name | extension}}">
                    <p><i class="fa"></i></p>
                    <a href="{{model.src}}" target="_blank" title="{{model.name}}">
                        {{model.name | filename }}.{{model.name | extension}}
                    </a>
                </div>
            </div>
        </div>
        <p ng-show="!model">Drop file here</p>
        <div nv-file-drop uploader="uploader" class="input-file-container">
            <button class="btn btn-success btn-large">
                <i class="fa fa-upload"></i> {{ buttonText || 'Upload File' }}
            </button>
            <input class="browse-image-btn" type="file" nv-file-select
                   input-validation-upload ng-model="model"
                   uploader="uploader" accept="{{fileTypeAccept}}"
                   required="required"/>
        </div>
    </div>
</div>

将文件放在此处

{{buttonText | |'上载文件'}
我在编写自己的指令时也经历了同样的行为。我的解决方案是根本不使用指令的作用域,而是使用函数更新作用域

指令:

app.directive('sfTableHeader', function() {
  return {
    restrict: 'A',
    scope: false,
    template: '<div><a ng-click="updateScope(1234567)">click me</a></div>'
  }
});

我在编写自己的指令时也经历了同样的行为。我的解决方案是根本不使用指令的作用域,而是使用函数更新作用域

指令:

app.directive('sfTableHeader', function() {
  return {
    restrict: 'A',
    scope: false,
    template: '<div><a ng-click="updateScope(1234567)">click me</a></div>'
  }
});

抱歉,它没有回答问题:)。我必须为我的指令传递一个模型和属性,并使用一个范围。对不起,它没有回答问题..:)。我必须为我的指令传递模型和属性并使用范围。变量
uibScopeModel
在控制器(ng控制器)中定义
$scope
?@stepankasysanenko-uibScopeModel在指令控制器/链接中定义。它的值是作为字符串的ng模型-我这样做是为了执行。直到我找到这个问题的解决方案。对不起,我不完全理解。你想实现什么?@stepankasysanenko-我正在尝试进行双向绑定,但当我将指令放入uib模式时,它不起作用,我必须将模型注入uib模型范围(使用
eval('$scope.$parent.$parent.'+$scope.uibScopeModel+'=$scope.model')
)你能创建JSFIDLE吗?我认为这可能与基本类型变量有关。变量
uibScopeModel
在控制器(ng控制器)中定义
$scope
?@stepankasysanenko-uibScopeModel在指令控制器/链接中定义。它的值是作为字符串的ng模型-我这样做是为了执行。直到我找到这个问题的解决方案。对不起,我不完全理解。你想实现什么?@stepankasysanenko-我正在尝试进行双向绑定,但当我将指令放入uib模式时,它不起作用,我必须将模型注入uib模型范围(使用
eval('$scope.$parent.$parent.'+$scope.uibScopeModel+'=$scope.model')
)你能创建JSFIDLE吗?我认为这可能是基元类型变量的问题。