得到正确的指数随拼接() [英] Get correct index with Splice()

查看:148
本文介绍了得到正确的指数随拼接()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图删除我的AngularJS视图中的项目。从视图中,我路过指数和放大器;该ID。在JS控制器,I 拼接(指数)的从AngularJS阵列/型号。而我通过ID到的 $ http.get 的调用上删除对我的数据库。这一切工作。

...

我使刷新我的网页的请求。每5秒我做更新角模型的要求,我Concat的任何新数据给它。但我发现它是搞砸了我的索引值。我以为这是我的重新排序指标:和阅读<一href=\"http://stackoverflow.com/questions/12461133/why-does-the-splice-method-have-this-strange-behavior\">this StackOverflow的线程帮助确认。

会出现什么情况是,我推删除页面上的第三个项目,并从数据库中删除该ID。但是,从页面拼接了错误的项目,直到我刷新页面。
如何确保我总是传递正确的索引?

查看

 &LT;李NG重复=新新闻|过滤器:查询|排序依据:orderProp&GT;
     &LT; D​​IV CLASS =newsitemID =newspost {{new.id}}&GT;
         &LT; H4 NG模型=news.title&GT; {{new.title}}&LT; / H4&GT;
         &LT; p NG模型=news.text&GT; {{new.text}}&LT; / P&GT;
         &LT;一类=BTN BTN-INFONG点击=可见=真正的&gt;查看文章&LT; / A&GT;
         &LT;一个NG点击=deleteNews(索引,new.id)级=BTN BTN-危险BTN-迷你GT&;&删除LT; / A&GT;
     &LT; / DIV&GT;
&LT; /李&GT;

controllers.js

  $ scope.deleteNews =功能(索引,ID){
    $ http.get('/ CI / index.php文件/新闻/ delete_news_ajax /'+ id)的.success(函数(){
        $ scope.news.splice(指数,1);
    });
};


解决方案

相反,通过索引和身份证,通

 &LT;一个NG点击=deleteNews(新)...&GT;

然后使用的indexOf 在你的控制器功能:

  $ scope.deleteNews =功能(newsItem){
   $ http.get('/ CI / index.php文件/新闻/ delete_news_ajax /+ newsItem.id))。成功(函数(){
      $ scope.news.splice($ scope.news.indexOf(newsItem),1);
   });
}

I'm attempting to delete items from my AngularJS view. From the view, I'm passing the index & the ID. In the JS controller, I splice(index) from the AngularJS array/model. And I pass the ID to a $http.get to call a delete to my database. This all works.

....

Until I make a request to refresh my page. Every 5 seconds I make a request to update the Angular model and I concat any new data to it. But what I'm finding is it screws up my index values. I assumed it was reordering my index: and reading this StackOverflow thread helped confirm that.

What would happen is that I push delete for the 3rd item on the page, and it deletes that ID from the DB. But it splices the wrong item from the page until I refresh the page. How do I make sure that I'm always passing the correct index?

View

<li ng-repeat="new in news | filter:query | orderBy:orderProp">
     <div class="newsitem" id="newspost{{new.id}}">
         <h4 ng-model="news.title">{{new.title}} </h4>
         <p ng-model="news.text">{{new.text}}</p>
         <a class="btn btn-info" ng-click="visible = true">View article</a>
         <a ng-click="deleteNews(index,new.id)" class="btn btn-danger btn-mini">Delete</a>
     </div>
</li>

controllers.js

$scope.deleteNews = function(index, id) {
    $http.get('/ci/index.php/news/delete_news_ajax/'+id).success(function(){
        $scope.news.splice(index, 1);
    });
};

解决方案

Instead of passing the index and id, pass new:

<a ng-click="deleteNews(new)" ...>

Then use indexOf in your controller function:

$scope.deleteNews = function(newsItem) {
   $http.get('/ci/index.php/news/delete_news_ajax/' + newsItem.id)).success(function() {
      $scope.news.splice($scope.news.indexOf(newsItem), 1);
   });
}

这篇关于得到正确的指数随拼接()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆