Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 新行未添加到组件中_Angularjs - Fatal编程技术网

Angularjs 新行未添加到组件中

Angularjs 新行未添加到组件中,angularjs,Angularjs,这里我使用角度树作为组件。当数据第一次输入时,它工作正常 但是数据来自后端,那么“添加行”按钮没有添加 这是我的密码 $scope.newSubItem = function(scope) { var nodeData = scope.$modelValue; nodeData.items.push({ id: nodeData.items.length?(nodeData.items[nodeData.items.length-1].

这里我使用角度树作为组件。当数据第一次输入时,它工作正常

但是数据来自后端,那么“添加行”按钮没有添加

这是我的密码

$scope.newSubItem = function(scope) {

        var nodeData = scope.$modelValue;
        nodeData.items.push({
            id: nodeData.items.length?(nodeData.items[nodeData.items.length-1].id)+1:nodeData.id * 10,
                    rowId: nodeData.rowId + '.' + ((nodeData.items.length?(parseInt(nodeData.items[nodeData.items.length-1].rowId.split('.').pop()))+1:1)),
                    items: []
        });
    };
错误是

 TypeError: Cannot read property 'push' of null
    at Scope.$scope.newSubItem (app.js:19)
    at $parseFunctionCall (angular.js:12332)
    at callback (angular.js:22949)
    at Scope.$eval (angular.js:14383)
    at Scope.$apply (angular.js:14482)
    at HTMLAnchorElement.<anonymous> (angular.js:22954)
    at HTMLAnchorElement.eventHandler (angular.js:3011)
TypeError:无法读取null的属性“push”
范围$Scope.newSubItem(app.js:19)
$parseFunctionCall(angular.js:12332)
回调时(angular.js:22949)
范围$eval(angular.js:14383)
在范围内$apply(angular.js:14482)
在兰开夏。(angular.js:22954)
在htmlanchorement.eventHandler(angular.js:3011)

在按下之前,如果数组为null,则需要初始化该数组

nodeData.items = [];

$scope.newSubItem = function(scope) {
   var nodeData = scope.$modelValue;
   if(nodeData && nodeData.items ){

   }        
};

这是因为当节点没有子元素时,您不会从服务器返回项目中的空数组

或者在服务器端添加代码,在项目中添加空数组

@Sajeetharan是对的,您需要在newSubItem函数中添加此代码

$scope.newSubItem = function(scope) {

    var nodeData = scope.$modelValue;
    if(!nodeData.items)
        nodeData.items =[];
    nodeData.items.push({
        id: nodeData.items.length?(nodeData.items[nodeData.items.length-1].id)+1:nodeData.id * 10,
                rowId: nodeData.rowId + '.' + ((nodeData.items.length?(parseInt(nodeData.items[nodeData.items.length-1].rowId.split('.').pop()))+1:1)),
                items: []
    });
};

如何重现调试窗口?:oNodeData上的条件将无效,他必须检查nodeData.items是否为null