0

This is my code.

.state('appSetting.studentList', { url: '/student', views: { 'list@appSetting': { templateUrl: appUrl + '/Student/List', controller: 'StudentCtrl' } } })
                .state('appSetting.studentList.details', { url: '/:studentId', views: { 'details@appSetting': { templateUrl: appUrl + '/Student/Edit', controller: 'StudentEditCtrl' } } })

                .state('appSetting.employeeList', { url: '/employee', views: { 'list@appSetting': { templateUrl: appUrl + '/Employee/List', controller: 'EmployeeCtrl' } } })
                .state('appSetting.employeeList.details', { url: '/:employeeId', views: { 'details@appSetting': { templateUrl: appUrl + '/Employee/Edit', controller: 'EmployeeEditCtrl' } } })

I have a add button on layout , when user click the add button call the following function.

 $scope.gotoAdd = function () {
        if ($state.current.name.indexOf(".details") == -1) {
            $state.go($state.current.name + ".details")
        }
        else {
            var paramId = $state.current.url.slice(2);
            $state.go($state.current.name, ({ studentId: "", reload: true }));
        }
    };

In the above code working fine, but when open employee page user click add button i want to empty of employeeId parameter. I have number of links so if condition is using lengthy process. The above variable paramId dynamically changed (one time "studentId" , "employeeId" ,..............) depends on current state. I tried the following code but not working because paramId contains string value

 $state.go($state.current.name, ({ paramId: "", reload: true }));

3 Answers 3

1

Why are you putting the second parameter in '( )' in $state.go().

Try

$state.go($state.current.next, {}, {reload: true});

or

$state.go($state.current.next, {paramId: ""}, {reload: true});

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

1 Comment

I already tried that code but not working when i write paramId:"" but still studentId not empty
1
$scope.gotoAdd = function () {
        if ($state.current.name.indexOf(".details") == -1) {
            $state.go($state.current.name + ".details")
        }
        else {
            for (var prop in $stateParams) {
                if ($stateParams.hasOwnProperty(prop)) {
                    $stateParams[prop] = '';
                }
            }
            $state.go($state.current.name,$stateParams, { reload: true });
        }
    };

Comments

1

$state.go can take an inherit boolean parameter that serve exactly for this purpose.

As of UI router 0.2.15 -probably an old version, the documentation states

inherit - {boolean=true}, If true will inherit url parameters from current url.

In fact, it reset not only url parameters, but all $stateParams to their default, like what they are in state definition.

This will keep only state params as passed through the params object and all other are set to default or deleted.

$state.go('target.state', params, {inherit: false});

Comments

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.