【发布时间】:2015-03-15 18:43:56
【问题描述】:
我是 Ionic 和 Angular 的新手,所以我不确定问题在两者之间的实际位置(如果有的话)。我似乎对 ng-repeat 不清除内存有问题。我写了一个简单的例子来演示
index.html
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>
<link href="//code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet">
<!-- ionic/angularjs js -->
<script src="//code.ionicframework.com/nightly/js/ionic.bundle.js"></script>
<script src="cordova.js"></script>
<script src="js/app.js"></script>
</head>
<body ng-app="starter">
<ion-pane>
<ion-header-bar class="bar-stable">
<h1 class="title">Ionic Blank Starter</h1>
</ion-header-bar>
<ion-content ng-controller="CardsCtrl">
<div ng-repeat="card in cards" on-tap="start(1000)">
<img ng-src="{{card.image}}">
</div>
</ion-content>
</ion-pane>
</body>
</html>
js/app.js
angular.module('starter', ['ionic'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
}).controller('CardsCtrl', function($scope) {
$scope.cards = [];
var addCards = function(_num, y) {
for (var x = 0; x < _num; x++)
{
$scope.cards.push({
image:"http://i.imgur.com/QR8mZAt.jpg?"+y
});
}
}
addCards(1, "new");
var cardDestroyed = function(y) {
//console.log("destroy");
$scope.cards.splice(0, 1);
addCards(1, y);
};
$scope.start = function(y){
console.log("start");
for (var x = 0; x < y; x++)
{
cardDestroyed(y);
}
console.log("stop");
}
})
每次单击图片时,ng-repeat 正在使用的数组都会删除一个元素并添加 1000 次。但是内存中存在一些问题,导致我的实际应用程序崩溃。
任何人都可以了解我是否做错了什么,或者它是否真的是一个错误?我浏览了 github 并没有看到与我所看到的相匹配的错误报告,一旦我确定这不仅仅是我做错了什么,我就会去报告。
【问题讨论】:
标签: javascript angularjs memory-leaks ionic-framework ng-repeat