javascript – 使用删除clicked元素并在angularjs中作用域

前端之家收集整理的这篇文章主要介绍了javascript – 使用删除clicked元素并在angularjs中作用域前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

HTML

JS

function BlogData($scope) {
  $scope.bloges = [
    {"name": "Nexus S","mobile": "858485454","description": "The nest to seehow it works"},{"name": "Motorola XOOM™ with Wi-Fi","mobile": "8584453454","description": "The nest to ytrb  dsfhgs gvd m seehow it works"},{"name": "MOTOROLA XOOM™","mobile": "443485454","description": "The nest bla bla  vd fg hvto seehow it works"}
  ];

  $scope.addTodo = function() {
    $scope.bloges.push({name:$scope.todoName,mobile:$scope.todoMobile,description:$scope.todoDesc,done:false});
    $scope.todoName = '';
    $scope.todoMobile = '';
    $scope.todoDesc = '';
  };

  $scope.removeTodo = function() {
    $scope.bloges.pop({name:$scope.todoName,done:false});
    $scope.todoName = '';
    $scope.todoMobile = '';
    $scope.todoDesc = '';
  };

}

var blogApp = angular.module('blogApp',[]);
blogApp.controller('BlogData',BlogData);

我在删除元素时遇到问题.当我点击删除删除最后一个元素.我也试过拼接,但无法取得成功.

这是一个Fiddle

一些关注与Angular实现相关: –

>我们需要使用表单操作ng-submit =“addTodo()”或者我们需要使用< button ng-click =“addTodo(”>请区分.
>任何人都可以在完全灵活的Web应用程序中以实用的方式定义角度的正确范围吗?

请指导我..谢谢!!

最佳答案
您可以尝试两个选项过滤器和拼接

过滤

HTML

添加ng-click =“theFilter(博客)

>使用_filter

喜欢:

JS

  $scope.theFilter = function(field) {
   $scope.bloges = _.filter($scope.bloges,function(nodeClient) {
      return !(nodeClient.name == field.name &&
             nodeClient.mobile == field.mobile &&
              nodeClient.description == field.description
             );
     });      
 };

Fiddle

其他方式是:

添加ng-click =“splice(博客,bloges)

拼接

HTML

JS

 $scope.splice = function(field,fields) {     
    fields.splice(fields.indexOf(field),1);
 };

Fiddle

作为旁注

_filter如何工作:

        filter_.filter(list,iterator,[context])
        别名:选择
        
        查看列表中的每个值,返回所有数组
        通过真值测试(迭代器)的值.代表们
        原生过滤器方法(如果存在).
      

var evens = _.filter([1,2,3,4,5,6],function(num){ return num % 2 == 0; });
=> [2,6]
原文链接:https://www.f2er.com/js/429820.html

猜你在找的JavaScript相关文章