User7 User7 - 1 month ago 20
AngularJS Question

AngularJs - getting undefined object when passing parameters through ui-sref

Hi I am very new to angularjs and here is my code

html

<table ng-table="tctrl.tableEdit" class="table table-striped table-vmiddle" show-filter="true">
<tr ng-repeat="w in $data" ng-class="{ 'active': w.$edit }">
<td data-title="'Company name'" filter="{ 'cl_company_name': 'text' }" sortable="'cl_company_name'">
<a ui-sref="clients.client-detail({ clientId: w.cl_id })"><span ng-if="!w.$edit">{{ w.cl_company_name }}</span></a>
<div ng-if="w.$edit"><input class="form-control" type="text" ng-model="w.cl_company_name" /></div>
</td>
</tr>
</table>


controller.js

.controller('TabsClientCtrl', function ($scope, $window, $stateParams) {

$scope.clID = $stateParams.clientId
alert($scope.clID)
})


state.js

.state ('clients', {
url: '/clients',
templateUrl: 'views/common.html'
})

.state('clients.client-detail', {
url: '/client-detail',
templateUrl: 'views/client-detail.html',
resolve: {
...
}
})


when I click on company name in td I get alert with undefined. Is that correct way to pass parameters using ui-sref?

Answer

Please change your route to

.state('clients.client-detail', {
    url: '/client-detail/?clientId',
    templateUrl: 'views/client-detail.html',
    resolve: {
         ...
    }
})

or

.state('clients.client-detail', {
    url: '/client-detail/:clientId',
    templateUrl: 'views/client-detail.html',
    resolve: {
         ...
    }
})

based on your requirement