如何包括一个谐音到其他没有创建一个新的范围是什么? [英] How to include one partials into other without creating a new scope?

查看:92
本文介绍了如何包括一个谐音到其他没有创建一个新的范围是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个路线。

// index.html
<div ng-controller="mainCtrl">
    <a href='#/one'>One</a>
    <a href='#/two'>Two</a>
</div>​​​​​​​​​
<div ng-view></div>

这就是我如何加载谐音为我的 NG-视图

// app.js
​var App = angular.module('app', []);​​​​​​​
App.config(['$routeProvider', function($routeProvider) {
    $routeProvider.when('/one', {template: 'partials/one.html', controller: App.oneCtrl});
    $routeProvider.when('/two', {template: 'partials/two.html', controller: App.twoCtrl});
  }]);

当我点击的链接,它让我看到NG-视图内适当的标记。但是,当我尝试包括的谐音/ two.html 的谐音/ one.html 使用 NG-包括,这也显示出正确的,但创建一个不同的范围,所以我不能够与它进行交互。

When I click the links, it shows me the appropriate markup inside the ng-view. But when I try to include partials/two.html inside partials/one.html using ng-include, it shows it properly but creates a different scope so I'm not able to interact with it.

// partials/two.html - markup
<div ng-controller="twoCtrl">I'm a heading of Two</div>

// partials/one.html - markup
<div ng-controller="oneCtrl">I'm a heading of One</div>
<div ng-include src="'partials/two.html'"></div>


如何解决这个问题呢?或是否有任何其他的方法来达到同样的效果?

​ How do I resolve this problem? Or Is there any other way to achieve the same result?

推荐答案

无须使用共享的服务,实际上,你可以做到这一点。 $范围。$放出(...)可以调度的事件到$ rootScope,这可以听他们和转播到子作用域。

You can actually do this without using a shared service. $scope.$emit(...) can dispatch events to the $rootScope, which can listen for them and rebroadcast to the child scopes.

演示: http://jsfiddle.net/VxafF/

参考:
http://www.youtube.com/watch?v=1OALSkJGsRw (见第一评论)

这篇关于如何包括一个谐音到其他没有创建一个新的范围是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆