AngularJS-拆分字符串问题 [英] Angularjs - Split String issue
问题描述
我暂时没有眼睛..下面的代码引发错误
I am temporarily devoid of seeing eyes.. The code below throws up an error
错误:value.split不是函数
Error: value.split is not a function
这是angularjs分割简单字符串的一种方法
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;
}
推荐答案
问题似乎是您有一个名为 value
的函数参数,该参数隐藏了外部变量 value
.另外,您的函数定义以)
而不是}
结尾,这是一个语法错误,不过,我认为这可能与您发布方法有关.代码在这里.
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.
您还应该显式声明变量 value1
和 value2
(除非您实际上在函数外声明了它们).
You should also explicitly declare the variables value1
and value2
(unless you have actually declared them outside your function).
尝试以下方法:
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;
};
这篇关于AngularJS-拆分字符串问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!