4

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 = "7,9";

$scope.getTemplate = function(value){

    var values = value.split(",");

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

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

1 Answer 1

7

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

4 Comments

The outer variable is a demonstration of what value looks like. The function receives a string parameter that looks like "x,y" and I need to split it (in function) to get x and y as separate values.. So I need that function parameter.
@PersistentNewbie Are you sure? Can you do a console.log(value) within the function to verify what it actually looks like?
the function is evoked from ng-click="tbGetTemplate(21,32) .. the console log only returns 21
Yes, thank you :) There are two methods I am calling,, one with one the other with two parameters,,, there was a bit missing for the second one. Thanks for pointing this out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.