search

Home  >  Q&A  >  body text

angular.js - Problem using the value of {{}} expression as dom attribute in AngularJS

I wrote a filter myself and the code is as follows:

app.filter('selected', function() {
      return function(input, value) {
        var out = "";
        if(input == value){
            out = "selected";
        }
        return out;
      };
    });

The page code is as follows:

            <select class="form-control input-sm" style="width: 60px;">
                <option {{data.page.pageSize|selected:5}} >5</option>
                <option {{data.page.pageSize|selected:10}} >10</option>
                <option {{data.page.pageSize|selected:20}} >20</option>
                <option {{data.page.pageSize|selected:50}} >50</option>
            </select>

But writing it like this doesn’t work now, and it has no effect when pageSize is any value. Can anyone give me some advice? Thanks!

高洛峰高洛峰2801 days ago668

reply all(2)I'll reply

  • 漂亮男人

    漂亮男人2017-05-15 17:05:19

    How about you stop using filter and change it like this?

     <select class="form-control input-sm" style="width: 60px;">
        <option ng-selected="data.page.pageSize === 5">5</option>
        <option ng-selected="data.page.pageSize === 10" >10</option>
        <option ng-selected="data.page.pageSize === 20" >20</option>
        <option ng-selected="data.page.pageSize === 50" >50</option>
    </select>

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-15 17:05:19

    <select class="form-control input-sm" style="width: 60px;">
        <option data-ng-selected="{{data.page.pageSize | selected: 10}}" >10</option>
        <option data-ng-selected="{{data.page.pageSize | selected: 20}}" >20</option>
        <option data-ng-selected="{{data.page.pageSize | selected: 50}}" >50</option>
    </select>
    
    
    app.filter('selected', function() {
          return function(input, value) {
            return input == value;
          };
        })

    reply
    0
  • Cancelreply