【发布时间】:2015-05-01 20:29:34
【问题描述】:
在我的列表下方,底部的 div 之一具有 removePortfolio 函数。该函数的作用是激活ng-hide="tickerRemoved",但仅针对该 1 个列表项,而不是所有列表项。
HTML 要点: https://gist.github.com/leongaban/cf72e5d0229155dd011f
指令要点: https://gist.github.com/leongaban/22a8feb9dbeea0b90135
<ul ng-show="loadingTickersDone" class="tickers-list">
<li class="ticker-li"
ng-repeat="ticker in tickers"
ng-hide="tickerRemoved"
ng-class="{'selected':toggleTicker.item == $index}"
ng-mouseleave="hideTickerOptions()">
<div class="ticker"
ng-click="toggleTicker.item = $index;
selectTicker(ticker.ticker);
revealTickerOptions()">
{{ticker.ticker}}
</div>
<div class="add-to-portfolio"
ng-show="tickerOptions"
ng-mouseleave="hideTickerOptions()">
<div ng-show="addOption"
ng-click="addPortfolio(ticker.ticker)">+ Portfolio</div>
<div ng-show="removeOption"
ng-click="removePortfolio(ticker.ticker)">- Portfolio</div>
</div>
</li>
</ul>
这是指令中的删除函数:
var vs = $scope;
vs.removePortfolio = function(ticker) {
this.tickerOptions = false;
ApiFactory.deleteWatchList(ticker).then(function(data) {
showMessage(ticker+' removed from portfolio!', data.data.status);
this.tickerRemoved = true;
});
};
this.tickerRemoved = true; 出现错误我认为这是因为范围在链中较低?
例如,我在此函数中使用this,它工作正常,因为该函数在标记/范围内更高:
vs.revealTickerOptions = function() {
this.tickerOptions = true;
if (tickerView === 'all') {
this.addOption = true;
this.removeOption = false;
}
else if (tickerView === 'port') {
this.addOption = false;
this.removeOption = true;
}
};
单击removePortfolio() 功能时,如何仅删除第一个<li class="ticker-li" 项目?
【问题讨论】:
-
this.tickerRemoved = true;的错误是因为在您的示例中,this指的是函数。您需要绑定函数:.then(function(data) { /* ... your code */ }.bind(this))。但是,它看起来会影响每个股票 - 您可能想要更像ticker.removed = true;和ng-hide="ticker.removed"的东西。然后,您还需要在ng-click代码中传递实际的ticker 对象:ng-click="removePortfolio(ticker)"。 -
啊,这真是个好主意!我刚刚尝试过,但遇到了这个错误
Cannot assign to read only property 'removed' of XOM打算再玩一下 -
忽略整个第一部分,这只是解释您遇到错误的原因。专注于后半段。传递
ticker对象,我假设它看起来像tickers =[{ticker: "aapl"}],传递整个事物以便您可以添加一个removed属性:{ticker: "aapl", removed: true},这样您就可以隐藏那个。无论是那个还是实际从数组中删除它,使用slice。 -
哦,成功了! :D 我需要在这里清理一些东西.. 秒,是啊@AlexMA 也有
标签: javascript angularjs angularjs-directive angularjs-scope angularjs-ng-repeat