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

前端 未结 2 2001
醉酒成梦
醉酒成梦 2020-12-29 02:41

Is it bad practice to declare both ng-app and ng-controller on the tag?

For example:

相关标签:
2条回答
  • 2020-12-29 02:59

    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.

    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.

    0 讨论(0)
  • 2020-12-29 03:08

    You should be able to access and set the title via the $window abstraction, thus removing the need to put a controller on the html tag.

    0 讨论(0)
提交回复
热议问题