角度中的数组映射错误
Array map error in angular
我使用角度ngTagsInput,我的列表是这样的:
[{text: "4353453"}, {text: "453453"}, {text: "4534534"}, {text: "5345"}]
并使用数组映射进行更改,如以下代码:
var array = [{text: "4353453"}, {text: "453453"}, {text: "4534534"}, {text: "5345"}];
var new_array = array.map(function(item) {
return parseInt(item.text);
});
当我在角度控制器中使用此代码时,我收到如下错误:
TypeError: Cannot read property 'map' of undefined
at new <anonymous> (CustomerPageController.js:208)
at Object.instantiate (angular.js:4619)
at angular.js:9870
at ui-bootstrap-tpls.min.js:8
at angular.js:15552
at m.$eval (angular.js:16820)
at m.$digest (angular.js:16636)
at m.$apply (angular.js:16928)
at g (angular.js:11266)
at t (angular.js:11464)
我该如何解决它?
不知道如何在$scope上设置标签,但考虑到它是在某些异步任务完成后设置的,
$scope.$watch('tags', function (tags) {
if (angular.isArray(tags)) {
$scope.post.phones = tags.map(function (tag) {
return parseInt(tag.text, 10);
});
} else {
$scope.post.phones = [];
}
});
我的完整控制器是:
AdminApp.controller('StoreCtrl', ['$scope', '$rootScope', '$modalInstance','$http','toaster', function ($scope, $rootScope, $modalInstance,$http,toaster) {
var maxTags = 6;
$scope.$watch('tags.length', function(value) {
if (value < maxTags)
{
$scope.placeholder = ' ' + (maxTags - value) + ' شماره دیگر میتوانید ثبت کنید';
}
else
{
$scope.placeholder = 'حداکثر ۶ شماره تماس';
}
});
var array = $scope.tags;
var new_array = array.map(function(item) {
return parseInt(item.text);
});
$scope.post.phones = new_array;
$scope.create = function(){
$http.post(AppCore.getGlobalApiUrl()+'actual_customer', {"name": $scope.post.name,"last_name": $scope.post.last_name,"phones": $scope.post.phones,"email": $scope.post.email,"default_address": $scope.post.default_address,"default_latitude": $scope.post.default_latitude,"default_longitude": $scope.post.default_longitude,"user_name": $scope.post.user_name,"password": $scope.post.password})
.success(function(response, status, headers, config){
$modalInstance.dismiss('cancel');
toaster.pop('success', "تعریف", "عملیات با موفقیت انجام شد");
setTimeout(function () {
window.location.reload();
}, 200);
$scope.posts.push(response.posts);
})
.error(function(response, status, headers, config){
$scope.error_message = response.error_message;
});
};
$scope.cancel = function () {
$scope.post = "";
$modalInstance.dismiss('cancel');
setTimeout(function () {
window.location.reload();
}, 200);
};
}]);
这是我
的html代码:
<tags-input placeholder="{{placeholder}}"
min-length="1"
max-length="11"
ng-class="{'read-input': tags.length > 6}"
allowed-tags-pattern="^[0-9]+$" max-tags="6" ng-model="tags"></tags-input>
<input type="hidden" ng-model="post.phones">
相关文章:
- 未捕获的引用错误:未定义<映射键>
- HERE 映射 javascript API - 抛出“指针必须有 ID”错误
- 谷歌未定义(api 映射错误)
- 函数给出错误转换未定义映射
- 在Struts2中从jsp中的表映射错误的行数
- 操作映射触发控制器中的错误方法
- 树映射导致未捕获类型错误:未定义不是函数
- Lodash partialRight padStart映射产生错误结果
- 创建映射时出现同源策略错误
- 将 JS 错误行号映射到未缩小的非捆绑资产文件
- 打开图层映射是未定义的错误
- 映射函数反应中的错误
- 角度中的数组映射错误
- 尝试在 Google 地图上映射点并收到错误(未处理的异常)
- 将 JSON 数组映射到 KO 抛出错误
- ChildBrowser和PhoneGap 1.5.0(Cordova)的映射错误
- 在将静态资源切换到CDN后,谷歌在Chrome+Firefox中映射错误,在Safari中很好:
- 谷歌在通过iframe嵌入时映射错误
- 在Windows RT上用phonegap上传mp3文件[Unicode映射错误]
- jQuery.返回对象属性映射时出现映射错误