0

I'm using angular 1.5 to build a directive that I can use in different situations. I want to be able to pass in data from the controller to the directive as an object. My set up is something like this:

Controller:

function ControllerWrapper($scope, $location, $window){
    $scope.depth= 99;
    $scope.filter = {filters: [{column: 'stage', values: ['Closed']}]};
} 

HTML:

<div ng-controller="ControllerWrapper">
    <my-great-directive max-depth="{{depth}}" filters="filter"></my-great-directive>
</div>

Directive return

return {
        restrict: 'E',
        templateUrl: function(element, attributes) {
            return attributes.templateUrl;
        },
        link: function(scope, element, attributes) {
            var obj = scope.$eval(attributes.filters);
            var dir = scope.$eval(attributes.dir);

            scope.$watch("filters",function(newValue,oldValue) {
                if(scope.vm.dir != null){
                    scope.vm.dir.rows = scope.filterTreeGrid();
                }
            }, true);

        },
        controller: DirectiveController,
        controllerAs: 'vm',
        bindToController: true,
        scope: {
            maxDepth: '@',
            filters: '=?'
        }
    };

From what I've read the filters: '=' should be enough to pass the data through but I'm getting a null after it tries to resolve it. Any suggestions?

Chris Maness
  • 1,682
  • 3
  • 22
  • 40
  • Show the code for trying to resolve it. – georgeawg Mar 20 '17 at 04:12
  • You know you have the isolate scope written as `filters: '=?'` -- is that intentional? Looks wrong, and could be the source of your problems... – Alexander Nied Mar 20 '17 at 04:35
  • @anied I was under the impression that = was the proper syntax for binding from the outer scope to that of the directive. According to this: http://stackoverflow.com/questions/21712147/angularjs-differences-among-in-directive-scope – Chris Maness Mar 20 '17 at 14:30
  • @ChrisManess -- yeah, sorry to be unclear-- I think the equal sign should be fine and correct, it's the question mark that's giving me pause. – Alexander Nied Mar 20 '17 at 14:42
  • @anied ok I took that out and I'm still having issues. So inside the watch function scope.$eval(attributes.filters); is resolving to undefined. But if I log out console.log(attributes.filters); it gives me a string of filter – Chris Maness Mar 20 '17 at 15:15
  • @georgeawg I've added some details to this case if you want to take another look over it. – Chris Maness Mar 20 '17 at 16:08
  • The code reference an attribute named `dir` but there is no such attribute defined in the template. Bindings will bind objects with no problems. There is no need to use `$watch`. **Eliminate any issues that aren't relevant to the problem.** See [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) – georgeawg Mar 20 '17 at 16:23
  • Possible duplicate of [Pass Directive Isolate Scope Bindings to its Controller \`this\` Context (AngularJS V1.6)](http://stackoverflow.com/questions/42867670/pass-directive-isolate-scope-bindings-to-its-controller-this-context-angularj) – georgeawg Mar 20 '17 at 18:39

0 Answers0