2017-12-20 10 views
0

Angular JS (1.x)에서 저의 작업을하고 있습니다. ModalService를 사용하여 모달을 작성 중입니다. 이제 모달에서 일부 필터를 사용하고 싶습니다. 같은 컨트롤러에 별도의 컨트롤러가 없다는 것을 고려하면 어떻게 사용할 수 있습니까?각도로 ModalService에 필터를 주입하는 방법?

다음은 당신이 여기에 달성하려는 것을 확실하지 않다 ModalService.js

angular.module('appServices').service('ModalService', ['$modal', 
    function ($modal) { 
    this.showModal = function (customModalOptions) { 
     return this.show(customModalOptions); 
    }; 

    this.createModalView = function(templateUrl, scopeFields) { 
     // bind the data prior to creating the modal overlay 
     function MyModalController($scope) { 
     angular.forEach(scopeFields, function(value, key) { 
      $scope[key] = value; 
     }); 
     } 

     MyModalController.$inject = ['$scope']; 

]); 

답변

0

코드입니다. 이를 위해

아래와 같이 그러나 필터가 서비스 및 지침에 주입 될 수 컨트롤러/서비스/지침에 이름 <filterName>Filter으로 종속성을 주입. 예 : number라는 필터는 numberFilter을 사용하여 주입 된 입니다. 주입 된 인수 은 값을 첫 번째 인수로 서식 지정하고 두 번째 인수로 시작하는 필터 매개 변수를 사용하는 함수입니다.

출처 : Angularjs docs.

0

그냥 문제가 해결되지 않을 경우, 사용 $filter를 주입하고 그것을 필터 이름

function ($filter, SomeService) { 
    let data = SomeService.getData(); 
    $scope.filteredData = $filter('myFilter')(data); 
} 

을 통과 $injector

$injector.get('$filter')('myFilter')(data); 
관련 문제