简体   繁体   English

在AngularJS中使用嵌套视图时如何保留引用?

[英]How do I keep a reference when using nested views in AngularJS?

I have 4 pages. 我有4页。
2 pages in English and 2 pages in French. 英文2页,法文2页。

Here are the 4 options : 这是4个选项:
EN > Page1 - Content: Computer ZH>第1页-内容: 计算机
EN > Page2 - Content: Keyboard 简体中文>第2页-内容: 键盘

FR > Page1 - Content: Ordinateur FR>第1页-内容: Ordinateur
FR > Page2 - Content: Clavier FR>第2页-内容: 键盘

How do switch a nested view for its equivalent? 如何切换嵌套视图的等效视图?

I have put together a plnkr. 我已经整理好了。
https://embed.plnkr.co/SZPp0C8guVeTtcLeWJ3T/ https://embed.plnkr.co/SZPp0C8guVeTtcLeWJ3T/

Any help is appreciated! 任何帮助表示赞赏! Thanks! 谢谢!


Expected Behaviour. 预期的行为。 When I click on "Francais", say I am on /en/page1/ , I want it to properly re-route to, in this case, /fr/page1 . 当我单击“ Francais”时,说我在/en/page1/ ,我希望它正确地重新路由到/fr/page1

预期行为


Actual Behaviour. 实际行为。 When I click on "Francais", say I am on /en/page1/ , it re-routes me to /fr and I don't know how to approach the problem at hand. 当我单击“ Francais”时,说我在/en/page1/ ,它会将我重新路由到/fr ,但我不知道该如何解决当前的问题。

实际行为

I forked a new plunker and updated it. 我派出了一个新的矮人,并对其进行了更新。 The challenge was to watch the scope of the route provider to see what prior values existed. 面临的挑战是观察路由提供者的范围,以查看存在哪些先前值。 Looking at the current state and name you can conditionally control in the controller which view is persisted. 查看当前状态和名称,您可以有条件地在控制器中控制保留哪个视图。 to do this I created a new function: 为此,我创建了一个新功能:

  //evaluate routes
  let checkRouteValues = (oldVal, newVal)=>{
    $scope.vm.message1 = oldVal;
    $scope.vm.message2 = newVal;

    //first check to see if language has changed
    if(newVal === 'english'&& oldVal ==='francais.page1'){
     $state.go('english.page1');
    }else if(newVal === 'english' && oldVal ==='francais.page2'){
         $state.go('english.page2')
    }else if(newVal ==='francais' && oldVal === 'english.page1' ){
        $state.go('francais.page1');
    }else if (newVal ==='francais' && oldVal === 'english.page2' ){
      $state.go('francais.page2');
    }
  }

Then added a watch to see when the state changed: 然后添加手表以查看状态何时更改:

 //added a watch to monitor the current state
  $scope.currState =$state;
  $scope.$watch('currState.current.name', function (newValue, oldValue){

     checkRouteValues(oldValue, newValue)
  });

The function checkRouteValues() monitors the new and old values and then sets the state based upon the condition. 函数checkRouteValues()监视新值和旧值,然后根据条件设置状态。 *caveat: I think this solution works for something small scale like this navigation here however if you had multiple pages I would look to go a different route. * caveat:我认为此解决方案适用于小规模的导航,例如此处的导航,但是,如果您有多个页面,我会选择不同的路线。

updated plunker 更新的插头

I did add some logging in the html so you could see the values change I did change the name of the app from theApp to app so be careful if you cpoy paste. 我确实在html中添加了一些日志记录,因此您可以看到值发生了更改,我确实将应用程序的名称从theApp更改为app,因此如果您cpoy粘贴,请小心。

I did use arrow functions in the code if you don't like arrow functions you can convert it back to 我确实在代码中使用了箭头功能,如果您不喜欢箭头功能,可以将其转换回

let checkRouteValues = function(oldVal, newVal){...}

I also used let declaration vs. var to isolate variable scope but you can use var as if you prefer. 我还使用了let声明与var来隔离变量作用域,但是可以根据需要使用var。

And finally with my changes change the route english, page one, is always the default loaded route when your app starts but this should give you enough to get you going. 最后,随着我的更改,更改路线英语(第一页)始终是您的应用启动时默认加载的路线,但这应该足以为您提供帮助。

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

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