Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript $state.go不';t通过参数_Javascript_Html_Angularjs_Angular Ui Router - Fatal编程技术网

Javascript $state.go不';t通过参数

Javascript $state.go不';t通过参数,javascript,html,angularjs,angular-ui-router,Javascript,Html,Angularjs,Angular Ui Router,这是我的HTML的一部分: <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> &

这是我的HTML的一部分:

<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>
ctrl.js:

//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});
//如果未设置“initData”,则默认设置它
如果(!localStorage.getItem('initData')){
$window.localStorage.setItem('initData',JSON.stringify($scope.initData));
}
$scope.retrievedData=JSON.parse($window.localStorage.getItem('initData'));
对于(变量i=0;i<$scope.retrievedData.length;i++){
$scope.retrievedData[i].birthdayDate=新日期().getFullYear()-新日期($scope.retrievedData[i].birthdayDate).getFullYear();
}
$scope.sortedType='firstName';
$scope.sortedReverse=false;
//删除行并更新本地存储键值
$scope.removeRow=函数(名称){
var index=$scope.retrievedData.findIndex(函数(obj){return obj.firstName==name});
$scope.retrievedData.splice(索引1);
setItem('initData',JSON.stringify($scope.retrievedData));
};
$state.go('edit',{obj:$scope.retrievedData});

因此,我创建了一个表,当用户单击“编辑”时,我需要将该对象传递到ui.router,以便在
customer details.html
中显示它。我怎么做?我在这里做错了什么。我已经阅读了ui.router上的所有文档,但不知道是否应该在初始控制器或其他一些控制器中定义
$state.go
。另外,我也遵循了这个问题,但无法使其正常工作:

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

app.js

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
            }
        });
});
内部控制器

function myController($state) {
   conrole.log($state.params.obj);
} 

在编辑状态下,您有两个参数,
id
obj
,其中一个在url内

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

$state.go('edit', {obj: $scope.retrievedData}); 
尝试将其添加到params对象中

params: {
   obj: null,
   id: null
}
编辑:

要回答您的进一步问题:

<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});
}
进入编辑状态
$scope.handleItem=功能(项目){
//此处从$scope.retrievedData提取特定于项目的数据,然后更改状态
var itemData=getItemData(item$scope.retrieveData);
$state.go('edit',{obj:itemData});
}

请参考'params:{'obj':null}`您是否尝试在app.config->state中添加类似的参数。这是我收到的消息:
angular ui router.min.js:9错误:Param值对状态'edit'无效。
是否在调用$state.go()之前填充了$scope.retrievedData?是。。但是这都是一个控制器,这可能是一个问题吗?我仍然收到这个错误:
angular ui router.min.js:9错误:参数值对状态“edit”无效。
应该在我的初始控制器中定义
$state.go()
,我的index.html在哪里,或者在我的视图在哪里?是的,你是对的,保留“conrole.log”($state.params.obj);'在您的初始控制器中。我只给出了示例,但id不是在ui sref中的html中传递的吗?对不起,您是对的。我传递了对象,但现在传递了整个对象,而我只需要传递单击的属性,这样我就可以在视图中插入它。我在控制台中传递对象:
object{#:null,id:“2”,obj:Array(5)} >但是我只需要点击被点击的一个。当你触发$STATE时,代码不清楚。无论如何,打包你的$STATE。进入一个方法并触发它点击按钮,通过哪个项目已经被CICICKDY的信息,这是我应该做的,也许是用NG点击。但是我怎样才能传递那个PAR的信息呢?特别的一件事,这正是困扰我的。。
<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});
}