剛剛開始使用Angular時,我有點掙扎,現在像foreach循環一樣簡單,看起來很容易?如果找到元素,則返回false,並且不要在下面做任何事情。在Angular Controller中退出一個foreach
$scope.addFav = function($text, $link, $icon)
{
var $favlist = $scope.favorites;
$favlist.forEach(function(element, index, array) {
console.log(element.name);
console.log($text);
if (element.name == $text)
{
console.log("Found");
return false;
}
});
$favlist.unshift({href: $link, name: $text, icon:$icon});
if($favlist.length > 5)
$favlist.pop();
$scope.favorites = $favlist;
return false;
};
我的使用情況
<i class="fa fa-star-o" ng-click="addFav(item.name, item.href, item.icon);"
我的問題是我在哪裏去了?控制檯被記錄爲「找到」很好,但它並沒有阻止其餘。
是否要在下面forEach停止代碼? – dfsq 2015-02-11 15:58:13