2014-02-22 4 views
0

각도로 필터를 생성하는 경우 tutorial을 통해 작업하고 있습니다. 나는 구현의 일부와 혼동스러워하고 근본적인 자바 스크립트 내려 온다. 대구 스 니펫은 다음과 같습니다.사용자 정의 필터 구문

// on the app object, the method filter is called passing a name parameter and the function 
myApp.filter('reverse', function() { 
    return function (text) { 
// now a function that accepts text as a parameter is returned  
return text.split("").reverse().join(""); 
// the text parameter splits, revers, and joins the text parameter. 
    } 
}); 

제 질문은 왜 이런 식으로 작성할 수 없습니까? 두 번째 줄의 첫 번째 스 니펫에서 함수를 반환하는 목적은 무엇입니까?

myApp.filter('reverse', function (text) { 
    return text.split("").reverse().join(""); 
}); 
+0

'filter'로 전달하는 함수가 종속성 삽입 가능하므로. – elclanrs

답변

1

filter에 전달하는 기능은 종속성 주사 가능합니다.

myApp.filter('uc', function($http, myService, text) { 
    return text.toUpperCase(); 
}); 

myApp.filter('repeat', function($http, $location, myService2, text) { 
    return text + text; 
}); 

text 인수 목록에있는 위치를 어떻게 알 수 있습니까 : 당신은 몇 가지를 주입 상상? 이제는 인수가 일치하지 않기 때문에 해당 필터가 작성되지 않습니다. 항상 첫 번째 인수로 값을 취하는 함수를 반환하면 모든 것이 잘 수행되며 다음을 수행 할 수 있습니다.

repeat(uc(value))