Jul*_*jan 14 javascript angularjs
我对AngularJS(版本1.2.6)有一个问题.
由于某些原因我无法理解我无法访问length存储在其中的字符串变量的属性$scope.
在模板中:
String '{{myObject.someVariable}}' has length '{{myObject.someVariable.length}}'.
Run Code Online (Sandbox Code Playgroud)
在控制器中:
$scope.myObject = {} ;
//asynchronuous loading of myObject
SomeService.loadObject(function(result)){
$scope.myObject = result ;
console.log("Content: '%s', length:'%i'",$scope.myObject.someVariable,$scope.myObject.someVariable.length);
$scope.$apply();
});
Run Code Online (Sandbox Code Playgroud)
结果是:
String 'aaaa' has length ''.
Run Code Online (Sandbox Code Playgroud)
在控制台中我正确地看到了 Content:'aaaa', length:'4'
这很令人讨厌,因为我根据字符串大小显示(或不显示)模板的某些部分.
更新
$scope.myObject.someVariable是一个字符串.我在回调函数中添加了两个监视器的断点:
$scope.myObject.someVariable :"aaaa"typeof($scope.myObject.someVariable) :"字符串"Mic*_*mza 21
是someVariable一个字符串?如果没有,您可能必须在访问length属性之前将其强制转换为字符串.任何简单的方法是用空字符串连接它:
{{(myObject.someVariable + '').length}}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
38103 次 |
| 最近记录: |