angular.element('#someId')。scope()在IE 9中返回未定义

时间:2015-12-24 09:37:36

标签: javascript jquery angularjs internet-explorer-9

我正在使用angular.js 1.2.13。我正在使用第三方工具,它使用angular.js并在我的项目中集成,我在.js文件中使用angular.js代码。

要获取范围数据,我在HTML元素上使用.scope()。它在chrome和mozilla中运行良好,但在IE 9上, angular.element('#someId')。scope()返回undefined。

我已经调试过,发现 angular.element('#someId')正在返回预期的元素,但 .scope()就是返回undefined。

在angular.js的网站上,写道1.2.X与IE9完全兼容。为什么我会收到此错误。

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>


//This code is written in external js file appConfig.js which is imported after angular.js
angular.module('modelerApp', []).run(['$rootScope', function($rootScope){

    //This function is defined by me
    $rootScope.bootstrap = function() {
        //some logic needs to be executed here which should happen only after page load, so I am keeping it inside rootscope
    }

}]);


<script type="text/javascript">
//This code is written in external js file which is getting imported in this page
  $(document).ready(function(){

    var scope = angular.element('#someId').scope(); //undefined

    //I need scope to access rootscope of angular in order to execute logic inside bootstrap method
    var rootScope = scope.$root; 

    //I want to execute logic written in bootstrap method, which is in $rootScope
    //This logic uses angular js routes and all, and thus needs to be inside angular code.
    //This has nothing to do with specific controller
    rootScope.bootstrap();

  });

</script>

<div ng-app="modelerApp" ng-controller="itemController">
  <div id="main">
    <div id="someId">
    </div>
  </div>
</div>

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

你试图访问角度范围之外的范围,你不是在角度的路径,你不应该使用角度与jQuery这种方式,但包括角度以上的jQuery,以利用jQuery而不是jqLit​​e of angular:< / p>

angular.module('modelerApp', []).controller('itemController', function($scope) {

  var scope = angular.element($('#someId')).scope(); //undefined
  $scope.obj = {
    scopeId: scope.$id
  };
  //console.log(scope.$id);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="modelerApp" ng-controller="itemController">
  {{obj.scopeId}}
  <div id="main">
    <div id="someId">
    </div>
  </div>
</div>

A detailed post about thinking in angular if one has a jquery background.