繁体   English   中英

Angular JS-不允许在转发器中捕获重复项的异常

[英]Angular JS - How to catch Duplicates in a repeater are not allowed exception

我需要验证添加到对象数组中的对象在对象的临时数组中不存在。

Angula JS投掷错误不允许在转发器中重复。 但是使用try / catch无法捕获此错误。

是否可以检查这种异常情况或检查对象数组中是否已经存在对象?

谢谢你的帮助。

代码示例:

  $scope.availableSounds = [
            {
                name: "Rain"
            },
            {
                name: "Storm"
            },
            {
                name: "Forest"
            },
        ];        

$scope.selectedSounds = [];

项的添加(所选声音的数组中的对象应唯一)

$scope.addSoundToSelection = function(index) {
            try {
                var selectedItem = $scope.availableSounds[index];
                $scope.selectedSounds.push(selectedItem);
                var pos = $scope.selectedSounds.map(function(e) { return e.hello; }).indexOf(selectedItem.name);
                console.log(pos);
            } catch(e) {
                $scope.showAlert();
            }
        };

indexOf方法怎么样

var myObjectToAdd = {};
var myArray = [ {}, {} ] //some array of objects

if (myArray.indexOf(myObjectToAdd) < 0) {
 myArray.add(myObjectToAdd);
}

或者,您可以使用lodash库的_.find

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM