简体   繁体   English

在html标签上声明ng-app和ng-controller是不是很糟糕?

[英]Is it bad to declare ng-app and ng-controller on html tag?

Is it bad practice to declare both ng-app and ng-controller on the <html> tag? <html>标签上声明ng-appng-controller是不好的做法吗?

For example: <html class="no-js" ng-app="myApp" ng-controller="MainCtrl"> 例如: <html class="no-js" ng-app="myApp" ng-controller="MainCtrl">

Is this considered bad practice? 这被认为是不好的做法吗? I am trying to control the <title> tag of my application dynamically, so I want to declare the MainCtrl controller early as it's scope is important in the rest of the application. 我试图动态控制我的应用程序的<title>标签,所以我想尽早声明MainCtrl控制器,因为它的范围在应用程序的其余部分很重要。

Then I can use <title>{{settings.title}}</title> in the MainCtrl controller and have child controllers access it via $scope.$parent.settings.title = "hello world"; 然后我可以在MainCtrl控制器中使用<title>{{settings.title}}</title> ,让子控制器通过$scope.$parent.settings.title = "hello world";访问它$scope.$parent.settings.title = "hello world";

您应该能够通过$window抽象访问和设置标题,从而无需在html标记上放置控制器。

I don't think there will be any problem so far. 到目前为止,我认为不会有任何问题。 The only impact is the rootscope no more readable from html markup, because the controller scope is overriding it. 唯一的影响是rootcope不再可以从html标记读取,因为控制器范围覆盖了它。

For example, 例如,

<div id="parent" ng-app>
  <div id="child" ng-controller='...'>

$rootScope.$id = 002    // rootscope from $rootscope service
$("#parent").scope().$id =002 // rootscope scope get from element scope    
$("#child").scope().$id =003  // controller scope get from element scope 

when it comes to the same markup, 当涉及到相同的标记时,

<div id="parent" ng-app ng-controller='...'>

$rootScope.$id = 002    // rootscope from $rootscope service
$("#parent").scope().$id =003 // controller scope get from element scope

Here you have no way to get rootscope from element scope, but who cares. 在这里你无法从元素范围获得rootcope,但是谁在乎。

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

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