Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
给定JSON和Angularjs中另一个对象的值,获取一个对象的值_Json_Angularjs - Fatal编程技术网

给定JSON和Angularjs中另一个对象的值,获取一个对象的值

给定JSON和Angularjs中另一个对象的值,获取一个对象的值,json,angularjs,Json,Angularjs,考虑到这一点: { "firstName": "John", "lastName" : "doe", "age" : 26, "address" : { "streetAddress": "naist street", "city" : "Nara", "postalCode" : "630-0192" }, "phoneNumbers": [

考虑到这一点:

{
    "firstName": "John",
    "lastName" : "doe",
    "age"      : 26,
    "address"  :
    {
        "streetAddress": "naist street",
         "city"         : "Nara",
         "postalCode"   : "630-0192"
     },
     "phoneNumbers":
     [
         {
             "type"  : "iPhone",
             "number": "0123-4567-8888"
         },
         {
             "type"  : "home",
             "number": "0123-4567-8910"
         }
     ]
 }
在Angularjs中,当type的值等于iPhone或home时,是否有方法获取number的值


我认为这对于像JSONPath这样的东西来说是很容易做到的,但是我很难让JSONPath很好地处理Angular。如果有一个简单的方法可以做到这一点的话,我喜欢听它

首先需要访问对象的PhoneNumber数组

像这样

obj = { ..your object.. }

var phoneNumbers = obj.phoneNumbers;
如果在标记中执行此操作,则可以使用带有管道语法的角度过滤器

 {{ expression | filter }}
如果在控制器中执行此操作,仍然可以使用具有控制器语法的角度过滤器:

$filter('filtername')(arg1,arg2);
这里是过滤器的链接

过滤器非常强大,您甚至可以编写自己的自定义过滤器来完成几乎所有您需要的操作


希望这有帮助。

您可以使用$filter服务,但不要忘记将其注入控制器。请看这里:


这个问题太模糊了,没有什么背景。我不确定我能说得更清楚。我想找出number或0123-4567-8888的值,因为我知道type的值是iPhone。在什么用例中?用户事件?显示过滤器?找一个还是找全部?仍然缺乏背景。通常,我会提供一些标记来提供我想要的内容!非常感谢。
 app.controller('firstCtrl', function($scope, $filter){

  $scope.user = {
    "firstName": "John",
    "lastName" : "doe",
    "age"      : 26,
    "address"  :
    {
        "streetAddress": "naist street",
         "city"         : "Nara",
         "postalCode"   : "630-0192"
     },
     "phoneNumbers":
     [
         {
             "type"  : "iPhone",
             "number": "0123-4567-8888"
         },
         {
             "type"  : "home",
             "number": "0123-4567-8910"
         }
     ]
 };
  //I've made assumption that only one number exist for iphone but of not you just get first one
  $scope.iphoneNumber = ($filter('filter')($scope.user.phoneNumbers, "iPhone"))[0].number;

});