0

I'm new to AngularJS. I have a custom filter like this.

app.filter('makeUppercase', function () {
  return function (item) {
    return item.toUpperCase();
  };
});

On ng-change I want to trigger my custom filter. Means, when user select option1 I want to trigger the above filter.

  <md-select ng-model="myModel" ng-change="" placeholder="Select an option">
2
  • I just want to trigger that. @31piy Commented Dec 28, 2017 at 10:48
  • @31piy plz help Commented Dec 28, 2017 at 11:00

3 Answers 3

1

Try this solution.

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $filter) {

  $scope.option = ["option01", "option02", "option03"];

  angular.element(document.querySelector('#option')).on('change', function() {    
    var a = this.selectedOptions[0].label;
    console.log(a);
    $scope.result = $filter('makeUppercase')(a);
    console.log($scope.result);
  })

});
app.filter('makeUppercase', function() {
  return function(item) {
    console.log("OK");
    return item.toUpperCase();
  };
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
  <select id="option" ng-model="selectedName" ng-options="x for x in option ">
</select>

</div>

Sign up to request clarification or add additional context in comments.

2 Comments

This is similar to what I want. I need small help. Can you help me with the ng-change="" ? Instead of using angular.element(document.querySelector('#option') this way, any options?
I try to do what you want but I couldn't do so that why I code this way.
0

I think you are trying to create a select list. So you can do like this.

<select ng-model="myModel" ng-options="obj as (obj | makeUppercase) for obj in objects" placeholder="Select an option">
</select>

Comments

0

Many ways to achieve this question but I would prefer a total Angular solution.Below is a HTML template based on what I understood from your question.

The moment you select a value from the drop down the ng-model value is turned into UPPERCASE from the filter you created.

HTML

<div role="main" class="container theme-showcase" ng-app="angularTable">
        <div class="" style="margin-top:90px">
            <div class="col-lg-8">
                <div class="page-header"></div>
                <div class="bs-component" ng-controller="listdata">
                    <select ng-options="x.username as x.username  for x  in users" ng-model="myModel" ng-change="optionChanged()"></select>
                    {{myModel | makeUppercase }}
                </div>
            </div>
        </div>
    </div>

JS

// Code goes here

var app = angular.module('angularTable', []);

app.controller('listdata', function($scope, $http) {

    $scope.users = [];
    $scope.myModel;

    $http.get("demo.json").success(function(response) {
        $scope.users = response;
        console.log($scope.users);
    });

    $scope.optionChanged = function() {

        console.log("options have been changed..");

    };

});

app.filter('makeUppercase', function() {
    return function(item) {
        if (item) {
            console.log(item)
            return item.toUpperCase();
        }
    };
});

Working DEMO

2 Comments

In ng change you have only done - $scope.optionChanged = function() { console.log("options have been changed.."); };
You can do what ever you want in ng-change function, I already applied the filter in HTML itself hence I left the function empty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.