Search code examples
angularjsangularjs-scopeangular-directiveangular-controller

angularJS parent scope not updating from directive


So here is my issue, I can't get my $scope.documents to update from my directive.$scope.parentUpdate({ documents: $scope.docType}) does not seem to execute at all an so my documents never updates. $scope.docType= resultgets all the data I need but it just does not push it back to the parent controller.

app.controller('docGridController',['$scope','getSideNav', 'getDocuments', 
            function($scope, getSideNav, getDocuments){

getSideNav().then(function(result){$scope.SideNav = result;
},
                    function(error){$scope.error = result;});

$scope.slideToggle = true;


$scope.documents=[];

$scope.update = function(k){

    $scope.documents = k;
    consle.log($scope.documents);
}}]);

app.directive('foSidenav',['getDocuments',function(getDocuments){

    return{

        replace: true,
        scope: {

            info:'=',
            docType:'=',
            parentUpdate:'&'
        },
        templateUrl:function(element,attr){

            return  attr.url;
        },
        controller: ['$scope','$element', '$attrs' ,'getDocuments',
                        function($scope,$element, $attrs, getDocuments){

            $scope.selectDocType = function(id)
            {
                getDocuments(id).then(function(result){$scope.docType= result;
                    console.log($scope.docType);
                    alert('Printed results');
                    $scope.parentUpdate({ documents: $scope.docType});
                    },
                    function(error){$scope.error = result;});


            };

        }]
    };
}]);

Here is the tag I am using in my template

<ul class="side-nav">
                    <li class="mainCat" ng-repeat=" item in info">
                        <a href="#" id="CatHeader" ng-click="slideToggle =! slideToggle">
                            <i class="{{item.displayIcon}} left-bar"></i>
                            <span ng-bind-html="item.label | html"></span>
                        </a>
                        <ul class="subCat slide-toggle" ng-show="slideToggle">  


                            <li ng-repeat="subItem in item.docTypes">
                                <a href="#" ng-click="selectDocType(subItem.id)" >
                                <i class="fi-folder"></i>
                                <span ng-bind-html="subItem.Name | html"></span>
                                </a>
                            </li>


                        </ul>
                    </li>           

and here is the directive call

<fo-sidenav url="{!URLFOR($Resource.FOPS_Resource, 'components/sidenav.html')}" info='SideNav' docType='documents' parentUpdate="update(documents)"></fo-sidenav>       

Any ideas?? these scope are really throwing me off


Solution

  • I think the issue is with how you are using the variables from your isolated scope in the html. Now you are using them like below:

    <fo-sidenav url="{!URLFOR($Resource.FOPS_Resource, 'components/sidenav.html')}"info='SideNav' docType='documents' parentUpdate="update(documents)"></fo-sidenav>
    

    Try like the following:

    <fo-sidenav url="{!URLFOR($Resource.FOPS_Resource, 'components/sidenav.html')}" info='SideNav' doc-type='documents' parent-update="update(documents)"></fo-sidenav>
    

    For more information about read Matching Directives and Normalization sections here