0

I have angular select on main page once user select value i want these object values in directive e.g $scope.selectedFileSize.value and $scope.selectedFileSize.size so i can further implement logic in directive. Any idea ?

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 message="event"></progress-bar-custom>

Controller.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);
    }

directive.js

angular.module("App").directive('progressBarCustom', function() {
            return {
                restrict: 'E',
                scope: {
                    message: "="
                },
                templateUrl: '/view/partials/progressbar.html',
                controller: function($scope) {
                    var data = $scope.message;
                    var currentFileBytes = [];
                    var currentBytesSum;
                    $scope.maxBytes = 3000; // how to get these values from controller 
                    $scope.max = $scope.maxBytes;
                    $scope.FileSizeString = $scope.selectedFileSize.value; //How can i get these values from controller.

                    $scope.random = function(value) {
                        $scope.dynamic = value;
                        $scope.downloadPercentage = parseFloat((value / $scope.maxBytes) * 100).toFixed(0);
                        console.log('current value-dynamic', $scope.dynamic);
                    };

                }
            });
2
  • you should take another scope property in as an attribute. Commented Aug 25, 2016 at 14:46
  • I need select ng-model value pass it to directive for that i still need an attribute on scope ? Commented Aug 25, 2016 at 15:03

4 Answers 4

2

You can define them as bindings in your directive scope:

scope: {
    message: "=",
    objToBind: "=" // add this one
},

And in HTML:

<progress-bar-custom message="event" obj-to-bind="selectedFileSize"></progress-bar-custom>

Then you could access it in your directive controller:

$scope.FileSizeString = $scope.objToBind.value

EDIT

I guess you want to dynamically change $scope.FileSizeString when your select is changed, right? Then I think you need to $watch in directive, otherwise it's always the initial value, and you won't aware of the changes in the future.

I don't know exactly how you implement your app, so I wrote a simple demo that demonstrate the key points:

  1. I moved your default select option into ng-options array, and instead use ng-init to set default option.
  2. I use $watch in directive to observe the binding's value change.

var app = angular.module('myApp', [])

app.controller('myCtrl', ['$scope', function($scope) {
  $scope.fileSizes = [
    {size: -1, value: 'Select'},
    {size: 1, value: '1MB'},
    {size: 2, value: '2MB'},
    {size: 3, value: '3MB'}
  ]

  $scope.onSizeChange = function() {
    console.log($scope.selected.size)
  }
}])

app.directive('myDirective', function() {
  return {
    restrict: 'E',
    scope: {
      selectedSize: '='
    },
    template: '<div style="font-family:monospace"><p><b>Your choice:</b> {{myChoice}}</p><p><b>Actual Choice:</b> {{selectedSize}}</p></div>',
    controller: function($scope) {
      $scope.myChoice = ''
      $scope.$watch('selectedSize', function (newVal, oldVal) {
        $scope.myChoice = (newVal && newVal.size !== -1) ? newVal.value : ''
      })
    }
  }
})
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="myCtrl">
    <select ng-options="opt as opt.value for opt in fileSizes"
            ng-model="selected"
            ng-init="selected = fileSizes[0]"
            ng-change="onSizeChange()">
    </select>
    <my-directive selected-size="selected"></my-directive>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

3 Comments

Yes i added attribute as per your answer but $scope.objToBind.value is undefined because user did not select option from dropdown how can i solve this ?
@hussain I updated my answer, is that what you were looking for?
Thanks for detail answer it helped me to resolve the problem.
1

Pass your required object through the isolate scope

HTML

<progress-bar-custom message="event" file="selectedFileSize"></progress-bar-custom>

JS

        restrict: 'E',
        scope: {
            message: "=",
            file: "="
        },
        templateUrl: '/view/partials/progressbar.html',

Comments

1

Since you have created an isolate scope in your directive, it is not normally recommended to use the $parent property, but rather to identify which variables you want to use from your parent scope. I would recommend that you pass in the variables you want to include in your directive in your html like so:

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

Then, in your directive in your scope attribute, you can add the variables.

scope: {
     message: "=",
     fileSize: "=",
     fileValue: "="
 },

Comments

0

You can use :-

scope.$parent.propertyName

inside your controller to access $scope level variables

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.