Skip to content Skip to sidebar Skip to footer

Angularjs - Split String Issue

I am temporarily devoid of seeing eyes.. The code below throws up an error Error: value.split is not a function Is threre an angularjs way of splitting a simple string var value

Solution 1:

The issue seems to be that you've got a function parameter named value which hides the outer variable, value. Also, your function definition ends with a ) rather than a }, which is a syntax error, although, I believe that's probably just in issue with how you've posted the code here.

You should also explicitly declare the variables value1 and value2 (unless you have actually declared them outside your function).

Try this instead:

var value = "7,9";

$scope.getTemplate = function(){
    var values = value.split(",");

    var value1 = values[0];
    var value2 = values[1];

    $scope.templateid = value1;
    $scope.userid = value2;
};

Or get rid of the intermediate variables entirely:

$scope.getTemplate = function(){
    var values = value.split(",");
    $scope.templateid = values[0];
    $scope.userid = values[1];
};

Update Given your comments it appears the method is actually being called with two parameters, rather than a single string parameter. In this case there's no need to split at all. Try this:

$scope.getTemplate = function(value1, value2){
    $scope.templateid = value1;
    $scope.userid = value2;
};

Post a Comment for "Angularjs - Split String Issue"