角度中的数组映射错误

Array map error in angular

本文关键字:映射 错误 数组      更新时间:2023-09-26

我使用角度ngTagsInput,我的列表是这样的:

 [{text: "4353453"}, {text: "453453"}, {text: "4534534"}, {text: "5345"}]

并使用数组映射进行更改,如以下代码:

 var array = [{text: "4353453"}, {text: "453453"}, {text: "4534534"}, {text: "5345"}];
 var new_array = array.map(function(item) {
 return parseInt(item.text);
 });

当我在角度控制器中使用此代码时,我收到如下错误:

TypeError: Cannot read property 'map' of undefined
at new <anonymous> (CustomerPageController.js:208)
at Object.instantiate (angular.js:4619)
at angular.js:9870
at ui-bootstrap-tpls.min.js:8
at angular.js:15552
at m.$eval (angular.js:16820)
at m.$digest (angular.js:16636)
at m.$apply (angular.js:16928)
at g (angular.js:11266)
at t (angular.js:11464)

我该如何解决它?

不知道如何在$scope上设置标签,但考虑到它是在某些异步任务完成后设置的,

$scope.$watch('tags', function (tags) {
    if (angular.isArray(tags)) {
        $scope.post.phones = tags.map(function (tag) {
            return parseInt(tag.text, 10);
        });
    } else {
        $scope.post.phones = [];
    }
});

我的完整控制器是:

AdminApp.controller('StoreCtrl', ['$scope', '$rootScope', '$modalInstance','$http','toaster', function ($scope, $rootScope, $modalInstance,$http,toaster) {
var maxTags = 6;
$scope.$watch('tags.length', function(value) {
    if (value < maxTags)
    {
        $scope.placeholder = ' ' + (maxTags - value) + ' شماره دیگر میتوانید ثبت کنید';
    }
    else
    {
        $scope.placeholder = 'حداکثر ۶ شماره تماس';
    }
});

var array = $scope.tags;
var new_array = array.map(function(item) {
    return parseInt(item.text);
});
$scope.post.phones = new_array;

$scope.create = function(){
    $http.post(AppCore.getGlobalApiUrl()+'actual_customer', {"name": $scope.post.name,"last_name": $scope.post.last_name,"phones": $scope.post.phones,"email": $scope.post.email,"default_address": $scope.post.default_address,"default_latitude": $scope.post.default_latitude,"default_longitude": $scope.post.default_longitude,"user_name": $scope.post.user_name,"password": $scope.post.password})
        .success(function(response, status, headers, config){
            $modalInstance.dismiss('cancel');
            toaster.pop('success', "تعریف", "عملیات با موفقیت انجام شد");
            setTimeout(function () {
                window.location.reload();
            }, 200);
            $scope.posts.push(response.posts);
        })
        .error(function(response, status, headers, config){
            $scope.error_message = response.error_message;
        });
};
$scope.cancel = function () {
    $scope.post = "";
    $modalInstance.dismiss('cancel');
    setTimeout(function () {
        window.location.reload();
    }, 200);
};
}]);
这是我

的html代码:

<tags-input placeholder="{{placeholder}}"
                        min-length="1"
                        max-length="11"
                        ng-class="{'read-input': tags.length > 6}"
                        allowed-tags-pattern="^[0-9]+$" max-tags="6" ng-model="tags"></tags-input>

            <input type="hidden" ng-model="post.phones">