UI-Router $ stateParams不返回值

时间:2018-04-04 03:08:18

标签: angularjs angular-ui-router

Angular 1.6.9,UI-Router 1.0.15

目标是使用解析器返回绑定到Angular组件的值。我有两个第三方示例,一个是UI路由器网站上的Hello Galaxy示例,另一个是Codepen中的一个简单案例。

https://plnkr.co/edit/XdKQmifZ69pcYeBnQ945?p=preview

请在Angular JS UI-Router State Params上查看David(@StretchKids)上的笔CodePen

然而,无论我如何剪切和粘贴,我都无法在我的Codepen项目中获得解析器功能来查看传递的对象。对象配置位于组件下的fred.js和ethel.js中。传递对象的按钮位于index.pug文件中(Codepen也使index.html可用)。

程序进入状态并执行解析器功能。但是$ stateParameters和$ transition $ .params()都是空的。啊!

该代码段不会执行。



   .module("MyApp")
   .component("fred", {
      templateUrl: "components/fred.html",
      bindings: { name: "@" },
      controller: function($stateParams){
         var vm = this;
         vm.activeId = $stateParams.id;
	      vm.resolveId = this.name;
      }
   })
   .config(function($stateProvider) {
      $stateProvider.state({
         name: "fred",
         url: "/fred",
         component: "fred",
         resolve: {
            name: function($transition$, $stateParams) {
               ;
               return $stateParams.id;
            }
         }

md-button(ng-repeat="name in ac.lvl1" ui-sref-active="active" ui-sref="{{name}}({id: 'bar'})") {{name}}




这是CodePen项目的链接:https://codepen.io/StretchKids/project/editor/AJPvQm#

HELP !!!

谢谢,

大卫

1 个答案:

答案 0 :(得分:0)

经过大量的搜索和测试后,我找到了答案。首先,我打破了UI-Router Hello Galaxy 示例。搜索所述传递的参数必须包含在URL中。

这解释了为什么 Hello Galaxy 示例传递了人的id,然后搜索了人物对象的数组,而不是只传递已经拥有的人物对象。绝对是在谷仓周围很远的地方!

我们需要在状态定义中添加一个params对象来提前声明$ stateParams变量。

{
         name: "person",
         url: "/{person.id}",
         component: "person",
         params:{data:null},
         resolve: {
            person: function($stateParams,$transition$) {
               return $stateParams.data.person;
            }
         }
      }

https://ui-router.github.io/guide/states#state-properties

https://ui-router.github.io/ng1/docs/1.0.14/interfaces/state.statedeclaration.html#params

http://benfoster.io/blog/ui-router-optional-parameters

这可能是小组中最好的:https://ui-router.github.io/guide/ng1/route-to-component

相关问题