0

I use angular ngTagsInput and my list is like:

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

And change it with array map like below code :

 var array = [{text: "4353453"}, {text: "453453"}, {text: "4534534"}, {text: "5345"}];

 var new_array = array.map(function(item) {
 return parseInt(item.text);
 });

When i use this code in my angular controller i got an error like :

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)

How can i fix it?

9
  • 1
    Your code seems to work fine. Commented Feb 22, 2016 at 16:37
  • This is not specific to angular, and does work as expected see this jsfiddle Commented Feb 22, 2016 at 16:38
  • 1
    Please, share a working example on plunkr or jsfiddle reproducing your problem. Commented Feb 22, 2016 at 16:38
  • I know it work fine but in my angular controller i don't know why not work Commented Feb 22, 2016 at 16:42
  • looks good, There is no problem in this piece of code Commented Feb 22, 2016 at 16:43

3 Answers 3

1

No idea how tags are being set on the $scope, but considering it is being set somewhere after some async task completes,

$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 = [];
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks , but this code posting just one number to DB ,for example if i typing 24234234,234234,2434234 one number stored
0

MY full controller is :

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

2 Comments

Where is $scope.tags defined? Looks like when you are trying to map $scope.tags, it is not defined yet. "undefined" has no method "map()".
why to post 2 answers for the single question ?
0

This is my html code :

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.