首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >$state.go不传递参数

$state.go不传递参数
EN

Stack Overflow用户
提问于 2017-06-29 17:53:10
回答 2查看 1.2K关注 0票数 1

这是我的HTML的一部分:

代码语言:javascript
复制
<td>
   <button class="btn btn-primary" ui-sref="edit({id: v.customerId})" ui-sref-opts="{reload: true}">Edit</button>
   <button class="btn btn-primary" ng-click="removeRow(v.firstName);">Delete</button>
</td>

正如您所看到的,我将customerId作为和id作为url中显示的参数之一进行传递

app.js:

代码语言:javascript
复制
var app = angular.module('webtrekkApp', ['ngSanitize', 'ui.router']);
app.config(function ($stateProvider, $urlRouterProvider) {
    $stateProvider
        .state('edit', {
            name: 'edit',
            url: '/users/:id/edit',
            templateUrl: './views/customer-details.html',
            controller: 'ctrl',
            params: {
                obj: null
            }
        });
});

ctrl.js:

代码语言:javascript
复制
//If 'initData' isn't set, then set it up by default
    if(!localStorage.getItem('initData')) {
        $window.localStorage.setItem('initData', JSON.stringify($scope.initData));
    }
    $scope.retrievedData = JSON.parse($window.localStorage.getItem('initData'));
    for (var i = 0; i < $scope.retrievedData.length; i++) {
        $scope.retrievedData[i].birthdayDate = new Date().getFullYear() - new Date($scope.retrievedData[i].birthdayDate).getFullYear();
    }
    $scope.sortedType = 'firstName';
    $scope.sortedReverse = false;
    //Remove Rows and Update localStorage Key Values
    $scope.removeRow = function(name) {
        var index = $scope.retrievedData.findIndex(function(obj) {return obj.firstName === name});
        $scope.retrievedData.splice(index, 1);
        window.localStorage.setItem('initData', JSON.stringify($scope.retrievedData));
    };
    $state.go('edit', {obj: $scope.retrievedData});

因此我创建了一个表,当用户单击“编辑”时,我需要将THAT object传递给ui.router,这样我就可以在customer-details.html中显示它。我该怎么做呢?我在这里做错了什么。我已经阅读了所有关于ui.router的文档,但不知道应该在初始控制器中还是在其他控制器中定义$state.go。我也关注了这个问题,但不能让它工作:How to pass custom data in $state.go() in angular-ui-router?

EN

回答 2

Stack Overflow用户

发布于 2017-06-29 18:18:45

在编辑状态下,您有两个参数idobj,其中一个在url中。

但是,当您从控制器触发状态时,您没有传递id参数,也没有定义默认值

代码语言:javascript
复制
$state.go('edit', {obj: $scope.retrievedData}); 

尝试将其添加到您的params对象中

代码语言:javascript
复制
params: {
   obj: null,
   id: null
}

编辑:

要回答您的进一步问题:

代码语言:javascript
复制
<button class="btn btn-primary" ng-click="handleItem(v);">Go to Edit state</button>

$scope.handleItem = function(item){
 //here extract your item specific data from $scope.retrievedData then change the state
 var itemData = getItemData(item, $scope.retrieveData);
 $state.go('edit', {obj: itemData});
}
票数 1
EN

Stack Overflow用户

发布于 2017-06-29 18:02:40

您好,您没有使用控制器,如果您想将参数传递给$state.go(),您应该让控制器在特定状态下获取该值。

app.js

代码语言:javascript
复制
var app = angular.module('webtrekkApp', ['ngSanitize', 'ui.router']);
app.config(function ($stateProvider, $urlRouterProvider) {
    $stateProvider
        .state('edit', {
            name: 'edit',
            url: '/users/:id/edit',
            templateUrl: './views/customer-details.html',
            controller: 'myController',
            params: {
                obj: null
            }
        });
});

在控制器中

代码语言:javascript
复制
function myController($state) {
   conrole.log($state.params.obj);
} 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44821847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档