下記のプログラムでわからないことがあり、教えて下さい。
HTML
1app.filter('myFormat',['hexafy', function(hexafy) { 2 return function(x) { 3 return hexafy.myFunc(x); 4 }; 5}]);
上記について、私のイメージだと
app.filter('myFormat',['hexafy', function(hexafy) { return hexafy.myFunc(x); }]);
でもいいような気がしたのですが、実際はエラーとなります。
return function(x) {の必要性の意味を教えてください.
また、
'myFormat',['hexafy', function(hexafy) の2つのhexafyはそれぞれ何を示しているのはよくわかっておりません。
HTML
1<!DOCTYPE html> 2<html> 3<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script> 4<body> 5 6<div ng-app="myApp" ng-controller="myCtrl"> 7<p>Use a filter when displaying the array [255, 251, 200]:</p> 8 9<ul> 10 <li ng-repeat="x in counts">{{x | myFormat}}</li> 11</ul> 12 13<p>This filter uses a service that converts numbers into hexadecimal values.</p> 14</div> 15 16<script> 17var app = angular.module('myApp', []); 18app.service('hexafy', function() { 19 this.myFunc = function (x) { 20 return x.toString(16); 21 } 22}); 23app.filter('myFormat',['hexafy', function(hexafy) { 24 return function(x) { 25 return hexafy.myFunc(x); 26 }; 27}]); 28app.controller('myCtrl', function($scope) { 29 $scope.counts = [255, 251, 200]; 30}); 31</script> 32 33</body> 34</html>
あなたの回答
tips
プレビュー