Javascript 类型错误:无法读取未定义的属性“concat”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27900035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:51:54  来源:igfitidea点击:

TypeError: Cannot read property 'concat' of undefined

javascriptangularjs

提问by ninjaXnado

My problem is that when i run my project it gives me an error that says: TypeError: Cannot read property 'concat' of undefined. it does not display please help.

我的问题是,当我运行我的项目时,它给了我一个错误:类型错误:无法读取未定义的属性“concat”。它不显示请帮助。

app.js

应用程序.js

    var Call24 = angular.module("Call24", ["ngResource", "ngRoute"]).
config(function($routeProvider) {
    $routeProvider.
        when('/', { controller: ListCtrl, templateUrl: 'CycleModal.html' }).
        when('/edit/:editId', { controller: EditCtrl, templateUrl: 'details.html' }).

        otherwise({ redirectTo: '/' });

    Call24.factory('cyclesFactory', function ($resource) {
return $resource('/api/Cycles/:id', { id3: '@id3' }, { update: 'PUT' });
});

 var ListCtrl = function ($scope, $location, data,$http) {

$scope.selectTest = null;
$scope.testTest = [];
 $scope.cycle = [];

$http({
    method: 'GET',
    url: '/api/Cycles/',
    data: { CycleID: 1 }
}).success(function (result) {
    $scope.testTest = result;
});

$scope.search = function() {
    data.query({
        offset: $scope.offset,
        limit: $scope.limit
        },
        function(something) {
            $scope.cycle = $scope.cycle.concat(something);
        });
};
   $scope.search();

$scope.show_more = function() {
    $scope.offset += $scope.limit;
    $scope.search();
};

 $scope.reset = function () {
   $scope.limit = 4;
   $scope.offset = 0;
   $scope.cycle = [];
   $scope.search();
 }
 };

html

html

Weeks in Cycle

周期中的周数

                    <div class="col-sm-3">
                        <div class="form-group">
                            <div class="input-group input-group-addon">
                                <label>Cycle number</label>
                                <input ng-model="query" class="form-control" placeholder="search         rep">
                                <!--<div class="input-group-btn">
                                    <button ng-click="reset()" class="btn btn-default"    type="submit"><i class="glyphicon glyphicon-search"></i></button>
                                </div>-->
                            </div>
                        </div>
                    </div>

                    <div class="col-sm-1">
                        <div class="form-group">
                            <div class="input-group input-group-addon">
                                <!--<label>Rep</label>
                                <input ng-model="part" class="form-control" placeholder="search rep">-->
                                <div class="input-group-btn">
                                    <button ng-click="reset()" class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>

  <table class="table table-striped table-condensed table-bordered">
    <thead>
    <th>Cycle Id</th>
    <th>Weeks In Cycle</th>
    <th>Cycle Number</th>
    <th>Start Date</th>
    <th>End Date</th>

    </thead>
    <tbody>
        <tr ng-repeat="liss in cycle">
            <td>{{liss.CycleID}}</td>
            <td>{{liss.WeeksInCycle}}</td>
            <td>{{liss.Cycle}}</td>
            <td>{{liss.StartDate}}</td>
            <td>{{liss.EndDate}}</td>

        </tr>
    </tbody>
</table>
<div class="form-actions">

    <button class="btn btn-primary" ng-click="updateSurvey()">
        Ok
    </button>
    <button class="btn btn-danger">
        <a href="#/index.html" style="color: white; text-decoration: none">Cancel</a>
    </button>
</div>

I want to display the list from a table but it keeps giving the error i have shown above.

我想显示表格中的列表,但它不断给出我上面显示的错误。

回答by dhavalcengg

Because $scope.cycle is not initialized

因为 $scope.cycle 没有初始化

$scope.cycle = '' or $scope.cycle= [] // as per your requirement

回答by Kalhan.Toress

seems like you didn't declare the $scope.cyclebefore its use, javascript try to invoke concat()of $scope.cyclevariable but still there is no variable called $scope.cycle. so invoking concat()of a undefined variable cause a error.

好像你没有声明$scope.cycle其使用之前,JavaScript的尝试调用concat()$scope.cycle变量,但仍然没有所谓的变量$scope.cycle。因此调用concat()未定义的变量会导致错误。

if you try to concat two arrays then

如果您尝试连接两个数组,则

try to add $scope.cycle = []top of the controller

尝试添加$scope.cycle = []控制器的顶部

if you try to concat two Strings then

如果您尝试连接两个字符串,则

try to add $scope.cycle = ''top of the controller

尝试添加$scope.cycle = ''控制器的顶部