AngularJS
$scope.orderByField = 'firstName';
$scope.reverseSort = false;
$scope.headers = ['First Name':'firstName','Last Name':'lastName']
View
<th
ng-repeat="(key,value) in headers"
ng-click="orderByField=value; reverseSort = !reverseSort">
{{ key }}
<span ng-show="orderByField == value">
<span ng-show="!reverseSort"> ▴</span>
<span ng-show="reverseSort"> ▾</span>
</span>
</th>
This is my code, I'm trying to create all table
headers from the list of keys in $scope.headers
and make each header sortable.
for some reason this code doesn't work, I don't get any errors in web console either.
There is the problem with nested scopes. Read about the nested scope problem in https://github.com/angular/angular.js/wiki/Understanding-Scopes .
Regarding how you fix it to create an object and use values from there:
$scope.modelData = {};
$scope.modelData.orderByField = 'firstName';
$scope.modelData.reverseSort = false;
$scope.headers = {'First Name':'firstName','Last Name':'lastName'}
Now, use accordingly:
<th ng-repeat="(key,value) in headers"
ng-click="modelData.orderByField = value; modelData.reverseSort = !modelData.reverseSort">
{{ key }}
<span ng-show="modelData.orderByField == value">
<span ng-show="!modelData.reverseSort"> ▴</span>
<span ng-show="modelData.reverseSort"> ▾</span>
</span>
</th>
Like @VVK answered, there is a problem in your code. Not sure what you are trying to define an Array
or an Object
,
Object should be defined like this: {'First Name':'firstName','Last Name':'lastName'}
(they should start with {
and end with }
but not with []
);
Change your HTML code to use modelData
and it will then work:
<tr ng-repeat="item in data | orderBy:modelData.orderByField:modelData.reverseSort">
Your $scope.headers
initialization is wrong. You should define it as
$scope.headers={'First Name':'firstName','Last Name':'lastName'}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.