AngularJS UI 状态和视图中指定的路由器参数对象之间的区别

Difference between AngularJS UI Router params object specified in state and views

我刚刚开始研究 AngularJS,特别是 Angular UI 路由器项目。在处理项目时,我观察到一些团队成员在 stateview 选项对象中指定了 params 选项对象。在这种情况下,它在通过 ui-sref/state.go 传递时不接受可选参数。 但是,我将此 params 选项对象移至状态而不是视图,并且可选参数功能开始工作。我正在使用 AngularJS 1.3.x 和 AngularJS UI 路由器版本 0.2.13。这是示例代码,可以更清楚地解释我想说的内容:

   $stateProvider.state('contacts', {
     url:"/user/{userId}/contact"
     views: {
        'view1': { 
            ....//other options
            params :{userId:0,contactId:null}
         }
     }
     ...//other options including `controller` and `resolve` options.          
   });

在上面的示例代码中(我给出了最少的 required 信息)params 对象是在 view1 对象而不是 contacts 状态上指定的。 contactId 是可选的非 URL 参数,它在一个用例上传递,而不在另一个用例中传递。但是,当我在状态上指定的控制器中检查 $stateParams 对象时,它只显示 userId 而不是 contactId 即使我通过了它。
当我将 params 选项对象从 view1 对象移动到 contacts 状态对象时,我解决了这个问题,如下所示:

    $stateProvider.state('contacts', {
     url:"/user/{userId}/contact"
     views: {
        'view1': { 
            ....//other options
            //i have removed the `params` object from here..
         }
     }
     ...//other options including `controller` and `resolve` options.          
     params :{userId:0,contactId:null} //and have put it here.
   });

现在我有以下问题:
1) 更改我指定 params 对象的位置有什么区别。如果有的话有什么意义?
2) 在视图中指定 params 对象是否完全错误的配置?如果是,那么为什么 UI 路由器不抱怨并使用 URL 中指定的参数? 如果不是那么为什么它不能使用可选的非 URL 参数?
3)任何特定的用例,人们更愿意在 view 对象上指定 params 选项对象而不是在 state 对象上?

当我将此 params 选项对象从 view 对象移动到 state 时,我发现的另一个副作用是我无法再将此 url 添加为书签或甚至在浏览器中刷新 url。当我这样做时,它会将我重定向到我们的主页。也许这就是我们在项目中处理这种重定向的方式。但只是想知道为什么会发生这种情况?(包括使用 ui-router 处理这种重定向的普遍程度)当然,我将深入研究我们的项目代码以了解为什么会发生这种副作用.
但是,我至少希望得到我在这里提出的 3 个问题(和子问题)的答案。

the guide nor the API documentation 都没有说命名视图可以有参数。所以我认为在视图中添加 params 只是一个错误,它没有任何效果。

所以,

1) What difference does it make by changing where I specify params object. What are the significance if any?

在视图中,ui-路由器不关心它。在该州,它执行 the API documentation.

中记录的操作

2) Is specifying params object on view altogether wrong configuration? If yes then why UI router doesn't complain and works with parameters specified in URL? If no then why it doesn't work with optional non-URL parameters?

是的,好像配置不对。

3) Any specific use cases one would prefer specifying params option object on view object than on state object?

没有