DD77
DD77

Reputation: 816

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?

Upvotes: 0

Views: 102

Answers (2)

Sravan
Sravan

Reputation: 18647

There are two thing to change in your routes,

1) Add the id parameter to the route url

url: '/client-detail/:clientId',

This :clientId is required to tell ui-router that you are sending some parameter via the route.

2) Add the controller:

controller: 'TabsClientCtrl

The controller is required as you are using those parameters in that controller.

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

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

Upvotes: 1

Vinoth Rajendran
Vinoth Rajendran

Reputation: 1211

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

Upvotes: 0

Related Questions