简体   繁体   English

一个root-app内有多个ng-app

[英]Multiple ng-app inside one root-app

I am new to angularjs.Using multiple ng-apps in same page. 我是angularjs的新手,在同一页面中使用多个ng-app。 like app2 inside app1 and app1 inside rootApp. 如app1内的app2和rootApp内的app1。

controller1 has one $http service it will retrieve the id and name and assign it to $rootScope . controller1有一个$http服务,它将检索ID和名称并将其分配给$rootScope

I want to use that values in controller2 in app2. 我想在app2的controller2中使用该值。

But the problem is both app1 called first and then app2 called immediately without getting id and name. 但是问题是app1首先被调用,然后app2立即被调用而没有获取id和name。

Because service takes some time to retrieve the id and name value. 由于服务需要一些时间来检索id和name值。

I want to call app2 after getting the id and name from app1. 我想从app1获取ID和名称后再调用app2。 How i can achieve this, 我如何做到这一点,

<body ng-app="rootApp">
...
<div ng-app="app1" ng-controller ="controller1">


.....

<div ng-app="app2" ng-controller="controller2">

</div>

</div>

</body>

var app1 = angular.module('app1', []); /

app1.controller('controller1', function ($scope, $filter, $rootScope) {

$http.get().success(function(data){
$rootScope.id=data.id;
$rooTScope.name=data.name;
});
});

var app2 = angular.module('app2', []); /

app2.controller('controller2', function ($scope, $filter, $rootScope) {

//here i want to use that $rootscopeValues of id and name

});

You can only have one ng-app and multiple ng-controllers. 您只能有一个ng-app和多个ng-controller。 Pay attention how you spell $rootScope. 请注意如何拼写$ rootScope。

var app = angular.module('app', [])
.controller('controller1', function($scope, $filter, $rootscope){
   $http.get().success(function(data){
     $rootScope.id = data.id;
     $rootScope.name = data.name;
   });

})
.controller('controller2', function($scope, $filter, $rootscope){
   console.log($rootScope.id);
   console.log($rootScope.name);
});

The document body: 文件正文:

<body ng-app="app">
...
<div  ng-controller ="controller1">


.....

<div ng-controller="controller2">

</div>

</body>

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

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