加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

AngularJS:将数据传递到角状ui路由器中的$state.go状态

发布时间:2020-12-17 09:27:44 所属栏目:安全 来源:网络整理
导读:我正在编写一个文档编辑器.文件可以是类型A或类型B.它们由URL通过文档id访问,但如果文档是A或B类型,则该ID不清楚. 所以,我需要通过id加载文档,从它的数据确定它的类型,然后传递给TypeAController或TypeBController. 现在,使用ui-router,我有这样的东西: $st
我正在编写一个文档编辑器.文件可以是类型A或类型B.它们由URL通过文档id访问,但如果文档是A或B类型,则该ID不清楚.

所以,我需要通过id加载文档,从它的数据确定它的类型,然后传递给TypeAController或TypeBController.

现在,使用ui-router,我有这样的东西:

$stateProvider
.state('loading',{
    url: '/{documentId}',template: 'Loading...',controller: function ($stateParams,$state) {
        loadDocument($stateParams.documentId)
            .then(function (loadedDocument) {
                if (loadedDocument.type === 'A') {
                    $state.go('EditA');
                } else if (loadedDocument.type === 'B') {
                    $state.go('EditB');
                }
            })
    }
})
.state('A',{...})
.state('B',{...})

加载状态加载文档,确定其类型,然后进入下一状态.

令人沮丧的是,我找不到一种实际将加载的文档传递给下一个州的方法!我可以做一个全球化的服务,我可以插入文档,或者我可以传递文档的id并在每个状态再次加载(希望从缓存这一次),但是这些方法是如此笨重,其他一切关于角度和角度ui已经如此顺利.

有什么建议么?

一个解决方案可能是将其移动到父状态,这对所有的孩子都是可用的.这样的事情
$stateProvider
.state('loading',controller: function ($scope,$stateParams,$state) {
        loadDocument($stateParams.documentId)
            .then(function (loadedDocument) {

                // assign the document to the parent model $scope
                // in this case $scope.model.doc  
                $scope.model = { "doc" : loadedDocument };
                if (loadedDocument.type === 'A') {
                    $state.go('.EditA');
                } else if (loadedDocument.type === 'B') {
                    $state.go('.EditB');
                }
            })
    }
})
.state('loading.EditA',{...}) // here we can use the $scope.model.doc
.state('loading.EditB',{...}) // in every child state

$scope.model.doc表示对共享文档的引用.

在这里(UI-Router example – contact.js),我们可以看到父母如何设置联系人集合,所有子状态正在访问它. example in action

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读