简体   繁体   English

AngularJS中的路由更改和控制器继承

[英]Route changing and controller inheritance in AngularJS

I have two questions: 我有两个问题:

  1. I have a form. 我有一张表格。 After the user pushed the save button, a put-request is fired to the server. 用户按下保存按钮后,将向服务器发出put-request。 The response contains the id of the new object. 响应包含新对象的id。 Now i wanna change the route from test.com/object to test.com/object/1. 现在我想改变从test.com/object到test.com/object/1的路线。 Since I have all informations, I don't wanna run the controller again (that would load the object again from the server, but this is useless). 由于我有所有信息,我不想再次运行控制器(这将再次从服务器加载对象,但这是没用的)。 Short version: How to change the route, without reloading the controller? 简短版本:如何在不重新加载控制器的情况下更改路径? If this is not possible, which is the best approach? 如果这是不可能的,哪种方法最好?

  2. I have two controller, which are very similar. 我有两个控制器,非常相似。 A good example is a createObject- and a editObject-Controller. 一个很好的例子是createObject-和editObject-Controller。 I wanna share almost every function, but there are some controller specific methods wich differ. 我想分享几乎所有功能,但有一些控制器特定的方法有所不同。 I love services, but in this case I don't see the big advantage, because I don't wanna share just the logic, I wanna share the definition of event-handlers itself. 我喜欢服务,但在这种情况下我看不到大的优势,因为我不想分享逻辑,我想分享事件处理程序本身的定义。 Short version: Which is the way to create inheritance for controllers? 简短版本:哪种方法可以为控制器创建继承?

I'm looking forward to answers and good ideas! 我期待着答案和好主意! Thanks a lot! 非常感谢!

Cheers 干杯

The typical (built-in) Angular way to change the route but not the view (or controller) is to only change the search part of your URL via $location.search('someSearchParam', searchParamValue) . 更改路径而不是视图(或控制器)的典型(内置)Angular方法是仅通过$location.search('someSearchParam', searchParamValue)更改URL的搜索部分。 reloadOnSearch has to be set to false for this to work properly. 必须将reloadOnSearch设置为false才能使其正常工作。 When the URL changes, event $routeUpdate is broadcast, so your controller can listen for this event to know that something has changed. 当URL更改时,将广播事件$ routeUpdate ,因此您的控制器可以侦听此事件以了解某些内容已更改。 See also AngularJS Paging with $location.path but no ngView reload 另请参阅AngularJS Paging with $ location.path但没有ngView重新加载

Controller inheritance is probably not a good idea. 控制器继承可能不是一个好主意。 You could use one controller and ng-show/hide to show the "create" or "edit" version of the page, as indicated by a $scope property, eg, $scope.editMode: 您可以使用一个控制器和ng-show / hide来显示页面的“创建”或“编辑”版本,如$ scope属性所示,例如$ scope.editMode:

<div> 
  <span ng-show="editMode">Edit Object</span>
  <span ng-hide="editMode">Create Object</span>
</div>
<div>Name: <input type="text" ng-model="name"></div>
...
<div>
  <input type="submit" ng-show="editMode" ng-click="update()">Update
  <input type="submit" ng-show="editMode" ng-click="create()">Create
</div>

If you do want to try controller inheritance, note that scopes inherit, not controllers. 如果您确实想尝试控制器继承,请注意范围继承,而不是控制器。 Controller scopes prototypically inherit from their parent scopes. 控制器范围原型继承自其父范围。 For more on this topic, see What are the nuances of scope prototypal / prototypical inheritance in AngularJS? 有关此主题的更多信息,请参阅AngularJS中范围原型/原型继承的细微差别是什么?

Thanks Mark, you helped me a lot! 谢谢马克,你帮了我很多忙!

The route change thing does not really satisfy me, but I think this is the only way for the moment. 路线改变的事情并不能让我满意,但我认为这是目前唯一的方法。 In my opinion it should be possible to change the route and not just the parameters, without reloading any controllers. 在我看来,应该可以改变路线而不仅仅是参数,而无需重新加载任何控制器。 Hopefully that will be possible in a future release! 希望在将来的版本中有可能!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM