声明多个应用程序时 'angular' 变量的范围?
scope of 'angular' variable when several apps are declared?
如果您有以下条件:
<html ng-app="outerApp">
<head ng-app="innerApp"></head>
<script>
var outerApp = angular.module("outerApp", []);
var ACtrl = outerApp.controller("ACtrl", function($scope){console.log($scope.name);});
var BCtrl = outerApp.controller("BCtrl",function($scope){console.log($scope.name});
var CCtrl = innerApp.controller.("CCtrl", function($scope){ console.log($scope.name);});
var innerApp = angular.module("innerApp", []);
</scope>
这样可以吗? angular 是一个全局变量,可以用于从 innerApp 和 outerApp 中声明模块吗?页面上的 ng-app 数量也有限制吗? ACtrl 和 BCtrl 是否都引用了相同的 $scope?
谢谢
这行不通,因为:
Only one AngularJS application can be auto-bootstrapped per HTML document. The first ngApp found in the document will be used to define the root element to auto-bootstrap as an application. To run multiple applications in an HTML document you must manually bootstrap them using angular.bootstrap instead. AngularJS applications cannot be nested within each other.
$rootscope,$scope 是概念上的全局变量,你可以将其实现为全局变量,用于在模块、指令、控制器、视图之间共享数据。
你应该从概念上阅读 DI(依赖注入)以及概念框架如何在 angular 中实现。你可以注入依赖项。
angular.module('modulename',[]);
[] 是一个数组,您在其中定义该模块依赖于另一个模块。换一种说法。
Angular 框架概念以可注入方式 DI(依赖注入)工作。
但是我强烈建议你,你试图实现的是正确的路径,你应该制作一个简单的自定义指令并将其作为依赖项注入 angular 应用程序。
如果您有以下条件:
<html ng-app="outerApp">
<head ng-app="innerApp"></head>
<script>
var outerApp = angular.module("outerApp", []);
var ACtrl = outerApp.controller("ACtrl", function($scope){console.log($scope.name);});
var BCtrl = outerApp.controller("BCtrl",function($scope){console.log($scope.name});
var CCtrl = innerApp.controller.("CCtrl", function($scope){ console.log($scope.name);});
var innerApp = angular.module("innerApp", []);
</scope>
这样可以吗? angular 是一个全局变量,可以用于从 innerApp 和 outerApp 中声明模块吗?页面上的 ng-app 数量也有限制吗? ACtrl 和 BCtrl 是否都引用了相同的 $scope?
谢谢
这行不通,因为:
Only one AngularJS application can be auto-bootstrapped per HTML document. The first ngApp found in the document will be used to define the root element to auto-bootstrap as an application. To run multiple applications in an HTML document you must manually bootstrap them using angular.bootstrap instead. AngularJS applications cannot be nested within each other.
$rootscope,$scope 是概念上的全局变量,你可以将其实现为全局变量,用于在模块、指令、控制器、视图之间共享数据。
你应该从概念上阅读 DI(依赖注入)以及概念框架如何在 angular 中实现。你可以注入依赖项。
angular.module('modulename',[]); [] 是一个数组,您在其中定义该模块依赖于另一个模块。换一种说法。 Angular 框架概念以可注入方式 DI(依赖注入)工作。
但是我强烈建议你,你试图实现的是正确的路径,你应该制作一个简单的自定义指令并将其作为依赖项注入 angular 应用程序。