JavaScript AngularJS Passing changing values from Controller to a Directive Controller

I'm pretty new with angular and I've read a lot of threads here and googled this topic but I cannot get a clear answer. What I'm trying to do is pass a value that is not set until the user makes a selection, at which point my controller will make a call an asynchronous call and assign the result to a value in the controller. My directive's controller needs to access this value to conduct its logic.

Here is some similar code to what I have.

app.controller('testCtrl', function($scope){
    $scope.getData = function(){
       //getDataFunc is a method in a Factory, which is not shown here
       $scope.results = getDataFunc();
    }
}

app.directive('testDir', function(){
    return{
        restrict: 'AE',
        scope: {
            resultData:  '='
        },
        controller:['$scope', function($scope){
            //I need to be able to access the $scope.results from the parent controller

        }
    }
}

Any help would be appreciated.

Answer:1

You are close:

Your markup should look something like:

<div ng-controller="testCtrl">
    <test-dir result-data="results"></test-dir>
</div>

app.controller('testCtrl', function($scope){
    $scope.getData = function(){
       //getDataFunc is a method in a Factory, which is not shown here
       $scope.results = getDataFunc();
    }
}

app.directive('testDir', function(){
    return{
        restrict: 'AE',
        scope: {
            resultData:  '='
        },
        controller:['$scope', function($scope){
            //I need to be able to access the $scope.results from the parent controller
             //$scope.resultData will always reflect the value of results here
             $scope.$watch('resultData', function(){
                 //called any time $scope.resultData changes
             });
        }
    }
}

You actually don't need two way binding, so this is how I would actually do it:

app.directive('testDir', function(){
    return{
        restrict: 'AE',
        scope: {
            resultData:  '&'
        },
        controller:['$scope', function($scope){
            //I need to be able to access the $scope.results from the parent controller
             //calling $scope.resultData() will always return the current value of the parent $scope.results

             $scope.$watch('resultData()', function(){
                 //called any time $scope.resultData() changes
             });
        }
    }
}
Answer:2

Consider sample html code: <head> .... <script src="/js/MyClass.js"></script> <script src="/js/extend.js"></script> <script> $(document)....

Consider sample html code: <head> .... <script src="/js/MyClass.js"></script> <script src="/js/extend.js"></script> <script> $(document)....

  1. method overloading javascript
  2. method overload js

In the following code, I have two Book Collections. One collection has "Moby Dick," while the other collection has "The Firm." However, when I look at the second collection, it also has Moby Dick. How ...

In the following code, I have two Book Collections. One collection has "Moby Dick," while the other collection has "The Firm." However, when I look at the second collection, it also has Moby Dick. How ...

Check here javascript:Function("alert('this is ok')")(); same as new Function("alert('this is ok')")(); What the syntax called ? Any reference ? EDIT Sorry, my fault,it's only a label,and it's ...

Check here javascript:Function("alert('this is ok')")(); same as new Function("alert('this is ok')")(); What the syntax called ? Any reference ? EDIT Sorry, my fault,it's only a label,and it's ...

  1. what does javascript do
  2. what does javascript mean
  3. what does javascript look like
  4. what does javascript void mean
  5. what does javascript need to work on a website
  6. what does javascript compile to
  7. what does javascript stand for
  8. what does javascript void(0)
  9. what does javascript code look like
  10. what does javascript do for a website
  11. what does javascript do in html
  12. what does javascript run on
  13. what does javascript error mean
  14. what does javascript eval do
  15. what does javascript map return
  16. what does javascript developer do
  17. what does javascript require do
  18. what does javascript trim do
  19. what does javascript await do
  20. what does javascript have to do with java

I am designing a game in HTML5 with Canvas/JavaScript. My aim is to create a timer that will countdown from a certain amount of minutes down to zero, during the time you need to shoot as many ...

I am designing a game in HTML5 with Canvas/JavaScript. My aim is to create a timer that will countdown from a certain amount of minutes down to zero, during the time you need to shoot as many ...

  1. create timer javascript
  2. creating timer js
  3. create countdown timer javascript
  4. create timer using javascript
  5. create simple timer javascript
  6. create timer event javascript