NG-重复和字符串替换前pression价值 [英] ng-repeat and string replace expression value

查看:151
本文介绍了NG-重复和字符串替换前pression价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到一个解决方案删除子在角前pression同时采用了NG-重复指令。

控制器处于外部JavaScript文件和HTML如下:

\r
\r

函数myController的($范围,$ HTTP){\r
  $ http.get(的http://本地主机:3000 / people.json')。\r
    成功(功能(数据){\r
      $ scope.persons =数据;\r
    });\r
}

\r

<表NG-控制器=myController的阶级=表的表条纹表悬停>\r
  < THEAD类=TEXT-中心>\r
    <第i个姓名和LT; /第i\r
    <第i个ID< /第i\r
    百分位>地址< /第i\r
  < / THEAD>\r
  <&TBODY GT;\r
    < TR NG重复=人者级=TEXT-中心>\r
      < TD>((person.name}}< / TD>\r
      &所述; TD> {{person.id}}&下; / TD>\r
      &所述; TD> {{person.address}}&下; / TD>\r
    < / TR>\r
  < / TBODY>\r
< /表>

\r

\r
\r

本地主机:3000 / people.json页面有几百JSON对象:

[
 {
   名称:约翰·史密斯
   ID:12345,
   地址:地址:789百老汇街
 }

...
]

我的问题:什么是剥出的最有效和/或最简​​单的方法:在地址值子地址?我没有到people.json页面的写访问。


解决方案

函数添加到您的控制器。像

  $ scope.stripAddr =功能(地址){
  返回address.substring(5);
}

和改变你的HTML

 < TD> {{stripAddr(person.address)}}< / TD>

请原谅,在我的JavaScript错误我用coffescript所有的时间,但我希望你的想法

I am unable to find a solution to remove a substring in an angular expression while using the ng-repeat directive.

The controller is in an external javascript file, and html is as follows.

function myController($scope, $http){
  $http.get('http://localhost:3000/people.json').
    success(function(data){
      $scope.persons = data;
    });
}

<table ng-controller="myController" class="table table-striped table-hover">
  <thead class="text-center">
    <th>Name</th>
    <th>ID</th>
    <th>Address</th>
  </thead>
  <tbody>
    <tr ng-repeat="person in persons" class="text-center">
      <td>((person.name}}</td>    
      <td>{{person.id}}</td>
      <td>{{person.address}}</td>
    </tr>
  </tbody>
</table>

The localhost:3000/people.json page has a couple hundred JSON objects:

[ { name: "John Smith", id: 12345, address: "addr:789 Broadway St" },

... ]

My Question: What is the most efficient and/or easiest method of stripping out the 'addr:' substring in the address value? I don't have write access to the people.json page.

解决方案

add a function to your controller. something like

$scope.stripAddr = function(address) {
  return address.substring(5);
}

and change your html to

<td>{{stripAddr(person.address)}}</td>

please excuse and errors in my javascript I use coffescript all time but I hope you get the idea

这篇关于NG-重复和字符串替换前pression价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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