1
我試圖在AngularJS中編寫自定義過濾器。此過濾器將大寫文本。上執行的代碼,這個錯誤已經拋出 「錯誤:value.split不是一個函數」錯誤:value.split不是函數
文件 「filter.js」:
"use strict";
angular.module("MyApp", []);
angular.module("MyApp").controller("UserController", ["$scope", function($scope)
{
this.infos =
{
firstname: "David",
lastname: "Michel",
cellphone: 123456
};
}]);
angular.module('MyApp').filter('capitalize', function()
{
return function(value){
var result = null;
var words = value.split(' ');
words.forEach(function(item){
if (result){
result += ' ';
}else{
result = '';
}
result += item.substr(0, 1).toUpperCase() + item.substr(1).toLowerCase();
});
return result;
};
});
文件 「filter.html」:
<html ng-app="MyApp">
<head>
<script type="text/javascript" src="angular.js"></script>
<script type="text/javascript" src="filter.js"></script>
</head>
<body>
<div ng-controller="UserController as user">
Firstname : {{user.infos.firstname | capitalize }}
Lastname : {{user.infos.lastname | capitalize }}
Cellphone : {{user.infos.cellphone | capitalize }}
</div>
</body>
</html>
我們可以看一下你是如何實現在HTML過濾器? –
@NathanBeck我剛剛更新了我的問題:文件「filter.html」 –
@NathanBeck,它的作品var words = value.toString()。split(''); –