【问题标题】:angularJS parent scope not updating from directiveangularJS父范围未从指令更新
【发布时间】:2016-02-06 19:30:23
【问题描述】:

所以这是我的问题,我无法让我的$scope.documents 从我的指令中更新。$scope.parentUpdate({ documents: $scope.docType}) 似乎根本没有执行,所以我的文档永远不会更新。 $scope.docType= result获取我需要的所有数据,但它不会将其推回父控制器。

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


            };

        }]
    };
}]);

这是我在模板中使用的标签

<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>           

这里是指令调用

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

有什么想法吗??这些范围真的让我失望

【问题讨论】:

    标签: angularjs angularjs-scope angular-directive angular-controller


    【解决方案1】:

    我认为问题在于您如何在 html 中使用隔离范围内的变量。现在您可以像下面这样使用它们:

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

    尝试如下:

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

    有关阅读匹配指令规范化部分的更多信息here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-04
      • 2015-09-16
      • 1970-01-01
      • 2015-11-06
      相关资源
      最近更新 更多