【问题标题】:AngularJS : update parent scope from directiveAngularJS:从指令更​​新父范围
【发布时间】:2015-09-29 20:09:50
【问题描述】:

我有一个角度控制器和指令

控制器

app.controller('gridController',['$scope',function($scope){                     
                    $scope.btns=false;
                    $scope.details_tab=false;
                    $scope.currentid={id:''};
                    $scope.setId=function(id){
                        $scope.currentid=id;
                    };
}]);

指令

       .directive('jqGrid',function(){

                    return{
                        restrict: 'E',
                        //require: 'ngModel',

                        link:function(scope, element, attrs){
                            jQuery("#jqgrid").jqGrid({
                                data : scope.jqgrid_data,
                                datatype : "local",
                                height : 'auto',
                                colNames : ['case', 'Name', 'Assigned Date'],
                                colModel : [
                                    { name : 'act', index:'act', sortable:false }, 
                                    { name : 'id', index : 'id' }, 
                                    { name : 'date', index : 'date', editable : true }, 
                                    { name : 'name', index : 'name', editable : true }, 
                                    { name : 'amount', index : 'amount', align : "right", editable : true }, 
                                    { name : 'tax', index : 'tax', align : "right", editable : true }, 
                                    { name : 'total', index : 'total', align : "right", editable : true }, 
                                    { name : 'note', index : 'note', sortable : false, editable : true }],
                                rowNum : 10,
                                rowList : [10, 20, 30],
                                pager : '#pjqgrid',
                                sortname : 'id',
                                toolbarfilter: true,
                                viewrecords : true,
                                sortorder : "asc",
                                gridComplete: function(){

                                },
                                multiselect : true,
                                autowidth : true,
                                onSelectRow : function (rowid, status) {
                                              scope.btns=true;
                                               scope.details_tab=true;
                                                scope.currentid='';
                                                }

        })}});

html:

            <jq-grid jqdata="jqgrid_data">
                <table id="jqgrid" ></table>
            </jq-grid>

在指令中我想更新范围内的变量 它在本地范围内更新,但不在控制器范围内。如何直接更新范围?

【问题讨论】:

    标签: javascript angularjs angularjs-directive angularjs-scope


    【解决方案1】:

    scope: true 添加到返回的指令对象中。

    .directive('jqGrid',function(){
        return {
            restrict: 'E',
            scope: true,
            ...
    

    【讨论】:

      【解决方案2】:
      app.controller('gridController',['$scope',function($scope){                     
                          $scope.btns=false;
                          $scope.details_tab=false;
                          $scope.currentid={id:''};
                          $scope.setId=function(id){
                              $scope.currentid.id=id; // change 
                          };
      }]);
      
        onSelectRow : function (rowid, status) {
                                                    scope.btns=true;
                                                     scope.details_tab=true;
                                                      scope.currentid.id=rowid;
                                                      }
      

      【讨论】:

        【解决方案3】:

        我用

        解决了这个问题
                   scope.$apply();
        

        【讨论】:

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