1

I have a scope dumping into a table via an ng-repeat. This data is filtered by 3 different things, 2 selects and a text box. All of this works fine, but I need to echo out how many results are in the table. Using {{ sounds.length }} obviously doesn't work because this is a count prior to the filtering and filtering has no effect on that. so i added a variable sound in soundsres = (sounds | filter: filters here) but doing {{ soundsres.length }} echos out nothing with no errors in the console.

Below is a complete set of code. What am I missing here. all indications (i even checked the docs and several older threads here on SO) indicate that this should be working. Thanks in advance.

Code:

<input type="text" ng-model="gearsearch">
<select ng-model="stypesearch"><option>1</option></select>
<select ng-model="stypesearch2"><option>2</option></select>

<span>{{ soundsres.length }}</span>

<table ng-controller="GearController">
  <tr ng-repeat="sound in soundsres = (sounds | filter: gearsearch | filter: stypesearch | filter: stypesearch2)">
    <td>{{ sound.id }}</td>
    <td>{{ sound.model }}</td>
    <td>{{ sound.make }}</td>
    <td>{{ sound.type }}</td>
    <td>{{ sound.class }}</td>
    <td>{{ sound.status }}</td>
    <td>{{ sound.cost | currency }}</td>   
  </tr>
</table>
3
  • I think that's a scoping issue. If you're placing your GearController to a div above gearsearch input and include the form and table in the same div it should work. Commented Nov 10, 2015 at 20:15
  • ah could be part of the issue. the problem is that gearsearch is a global search box that actually exists outside of the view itself. you can use gearsearch to filter any table in the entire application by leaving text in the box and switching views so i cant put gearsearch in the same div unfortunately Commented Nov 10, 2015 at 20:18
  • Oh OK, I see. I'll write an answer for you. Commented Nov 10, 2015 at 20:27

1 Answer 1

1

You could use controllerAs syntax and add the soundRes to your MainController or you could use $parent.soundRes to add your filter result to parent scope. I think the controllerAs method is more clear but both will work.

Please have a look at the demo below or in this fiddle.

(Sorry for my poor data model in the demo but I don't have a better one. But it's OK to show that the length is correctly updated.)

angular.module('demoApp', [])
	.controller('ViewController', function() {
		var vm = this,
        sameDate = new Date();
    
    sameDate.setMinutes(sameDate.getMinutes() - 5);
    var dateLimit = new Date(sameDate);
    
    var dates = [
        {
            name: 'test1',
        	date: new Date(sameDate)
        },
        {
            name: 'test2',
        	date: new Date(sameDate)
        },
        {
            name: 'test3',
        	date: new Date()
        }];
        
    vm.dates = dates;
	})
	.controller('MainController', MainController);

function MainController() {
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="demoApp" ng-controller="MainController as mainCtrl">
    <input ng-model="mainCtrl.searchText"/>
    <select ng-model="mainCtrl.stypesearch"><option>1</option></select>
    <select ng-model="mainCtrl.stypesearch2"><option>2</option></select>

    <!--results: {{mainCtrl.filterRes.length}}-->
    results: {{filterRes.length}}
    <ul ng-controller="ViewController as viewCtrl">
        <li ng-repeat="date in $parent.filterRes = ( viewCtrl.dates | filter:mainCtrl.searchText | filter: mainCtrl.stypesearch | filter: mainCtrl.stypesearch2 )">{{date}}</li>
    </ul>
</div>

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

1 Comment

works great! really the only thing i was missing in this case was $parent. in my repeat query. changing ` sound in soundsres = (...)` to sound in $parent.soundsres = (...) did the trick! Thank you!

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.