1. 程式人生 > >angular 自定義過濾器>>擷取字串、去除字串中的空格

angular 自定義過濾器>>擷取字串、去除字串中的空格

<!DOCTYPE html>
<html ng-app="myApp">

<head>
  <meta charset="UTF-8">
  <title></title>
  <script src="js/angular.min.js"></script>
</head>

<body ng-controller="myCtrl">


<div>
  <span>擷取字串:</span>
  <span>{{name|myfilter}}</
span> </div> <div> <span>去除文字中的空格(文字中間和開頭):</span> <span>{{urlstr|myurl}}</span> </div> <script type="text/javascript"> var app = angular.module('myApp', []); app.controller('myCtrl', ['$scope', function($scope) { $scope.name = 'xiaoming;xiaobei'
; $scope.urlstr = ' ww w.baodu.com'; } ]); /* app.filter('myfilter',function(){ return function(value){ var num = value.indexOf(';') return value.substr(0,num); } });*/ app.filter('myfilter', function() { return function(value) { return value =value.split(';')[0]; } }); app
.filter('myurl', function() { return function(value) { return value = value.split(" ").join(""); } }); </script> </body> </html>