如何从控制器中的ng-change方法为隔离范围赋值?

时间:2016-08-26 15:22:58

标签: javascript angularjs angular-directive-link

一旦用户从下拉列表中选择了值,我就调用了ng-change函数onSizeChange并设置了$scope.maxMb $scope.maxBytes $scope.FileSizeString的值,那么我的问题是如何使用这些值在指令中从下拉列表中选择一次值。我试图将这些值绑定到孤立的范围但没有运气。基本上我在大小选择之后需要fileSizefileValue我已经在html中将其作为属性添加到指令中,因此这些值应该绑定到隔离范围,但这种情况正在发生。我如何解决这个问题?

directive.js

angular.module("App").directive('progressBarCustom', function() {
    return {
        restrict: 'E',
        scope: {
            message: "=",
            fileSize: "=",
            fileValue: "="
        },
        templateUrl: '/view/partials/progressbar.html',
        controller: "StCtrl",
        link: function(scope, el, attrs) {
            console.log("file size", scope.fileSize);
            //these values should assign to directive template once user select value from dropdown
            //start 
            scope.maxMb = scope.fileSize;
            scope.maxBytes = 1000 * 1000 * scope.maxMb;
            scope.max = scope.maxBytes;
            scope.FileSizeString = scope.fileValue;
            // end 
            el.bind('click', function(event) {
                scope.$parent.startRecording();
                scope.$parent.stopLogs();
                scope.$parent.onSizeChange();
                console.log('EVENT', event);
            });
        };
    }
});

ctrl.js

  $scope.onSizeChange = function() {
       $scope.maxMb = $scope.selectedFileSize.size;
       $scope.maxBytes = 3000;
       $scope.max = $scope.maxBytes;
       $scope.FileSizeString = $scope.selectedFileSize.value;
       console.log('FileSize', $scope.maxMb);
   }

main.html中

<div class="col-md-3">
    <select class="form-control" ng-model="selectedFileSize" ng-options="item as item.value for item in FileSizeOptions" ng-change="onSizeChange()"><option value="">Select</option></select>
</div>

<progress-bar-custom ng-show="progressBarFlag" message="event" fileSize="selectedFileSize.size" fileValue="selectedFileSize.value"></progress-bar-custom>

template.html

<uib-progressbar type="success" class="progress-striped" max="max" animate="true" value="dynamic"><span>{{downloadPercentage}}%</span></uib-progressbar>
<p class="pull-right bytes-progress-0"><small>Recorded <strong>{{currentBytes}}</strong> of <strong>{{FileSizeString}}</strong></small></p>

1 个答案:

答案 0 :(得分:3)

将fileSize更改为file-size,将fileValue更改为file-value

<progress-bar-custom ng-show="progressBarFlag"     message="event" file-size="selectedFileSize.size" file-value="selectedFileSize.value"></progress-bar-custom>

与OP讨论后更新

在指令中传递selectedFileSize对象,而不是将其作为两个属性发送。您可以从指令中的selectedFileSize.size和selectedFileSize.value获取值。 然后在指令

中观察selectedFileSize对象