如何过滤与AngularJS一个数组,并使用过滤的对象的属性作为NG-模型属性? [英] How do I filter an array with AngularJS and use a property of the filtered object as the ng-model attribute?

查看:181
本文介绍了如何过滤与AngularJS一个数组,并使用过滤的对象的属性作为NG-模型属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有对象的数组,我想对角模型的基础上的过滤器中的一个元素的属性绑定,我该怎么做呢?我可以解释一个具体的例子更好的:

HTML

 <!DOCTYPE HTML>
< HTML NG-应用>
    < HEAD>
        &所述; SCRIPT SRC =htt​​ps://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js>&下; /脚本>
        <间的charset = UTF-8 />
        <标题> JS斌< /标题>
    < /头>
    <机身NG控制器=MyCtrl>
        <输入NG模型=results.year>
        <输入NG模型=results.subjects.title |过滤器:{等级:'C'}>
    < /身体GT;
< / HTML>

控制器:

 函数MyCtrl($范围){
  $ scope.results = {
    年份:2013,
    主题:[
      {标题:英语,等级:'A'},
      {标题:'数学',等级:'A'},
      {标题:科学,等级:'B'},
      {标题:地理,等级:'C'}
    ]
  };
}

JSBin: http://jsbin.com/adisax/1/edit

我要过滤的第二个输入一个档次'C'的主题,但我不想对模型绑定到的等级的;我想将它绑定到的标题的有品位'C'。

的主题

这是可能的,如果是这样,它是如何做的?


解决方案

 < D​​IV NG重复=主题中results.subjects |过滤器:{等级:'C'} >
    <输入NG模型=subject.title/>
< / DIV>

If I have an array of objects, and I want to bind the Angular model to a property of one of the elements based on a filter, how do I do that? I can explain better with a concrete example:

HTML:

<!DOCTYPE html>
<html ng-app>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
        <meta charset=utf-8 />
        <title>JS Bin</title>
    </head>
    <body ng-controller="MyCtrl">
        <input ng-model="results.year">
        <input ng-model="results.subjects.title | filter:{grade:'C'}">
    </body>
</html>

Controller:

function MyCtrl($scope) {
  $scope.results = {
    year:2013,
    subjects:[
      {title:'English',grade:'A'},
      {title:'Maths',grade:'A'},
      {title:'Science',grade:'B'},
      {title:'Geography',grade:'C'}
    ]
  };
}

JSBin: http://jsbin.com/adisax/1/edit

I want to filter the second input to the subject with a grade 'C', but I don't want to bind the model to the grade; I want to bind it to the title of the subject that has grade 'C'.

Is this possible, and if so, how is it done?

解决方案

<div ng-repeat="subject in results.subjects | filter:{grade:'C'}">
    <input ng-model="subject.title" />
</div>

这篇关于如何过滤与AngularJS一个数组,并使用过滤的对象的属性作为NG-模型属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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