GRowing GRowing - 6 months ago 23
AngularJS Question

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

$scope.getTemplate = function(value){

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

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

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

Answer

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