Angular JS

Customer Filter

봄산 2016. 8. 1. 19:49

1.기본구조


app.filter('myFilter', function() {


  // In the return function, we must pass in a single parameter which will be the data we will work on.

  // We have the ability to support multiple other parameters that can be passed into the filter optionally

  


return function(input, optional1, optional2) { //옵션값은 {{원본데이더터 | myfilter:option1,option2}} 이렇게 사용


    var output;


    // Do filter work here


    return output;


  }


});


2.예제

  -보드 ID을 입력 받아 게시판 이름을 리턴


app.filter('boardId', function() {


  // In the return function, we must pass in a single parameter which will be the data we will work on.

  // We have the ability to support multiple other parameters that can be passed into the filter optionally

  

  return function(input) {


    var output;


    // Do filter work here

    

    switch (input) {

    

    case 0:

        output = "공지사항";

        break;

    case 1:

        output = "FAQ";

        break;

    case 2:

        output = "병원뉴스";

        break;

}



    return output;


  }


});


// Setup the filter

app.filter('customCurrency', function() { 


  // Create the return function and set the required parameter name to **input**

  // setup optional parameters for the currency symbol and location (left or right of the amount)

  return function(input, symbol, place) {


    // Ensure that we are working with a number

    if(isNaN(input)) {

      return input;

    } else {


      // Check if optional parameters are passed, if not, use the defaults

      var symbol = symbol || '$';

      var place = place === undefined ? true : place;


      // Perform the operation to set the symbol in the right location

      if( place === true) {

        return symbol + input;

      } else {

        return input + symbol;

      }


    }

  }


});


사용법:{{ 25 | customCurrency:'$':false }}