search

Home  >  Q&A  >  body text

angular.js - angular.element方法如何获取到DOM元素所在的scope?

父页面a.html有一个controller,然后利用include函数将b.html加载进来。
父controller里面嵌套一个子controller。
然后发现element.scope方法只能获取到主controller的scope,不能返回元素所在的scope。
请教大神如何解决。

a.html

<script src="app.js" type="text/javascript"></script>
<p ng-controller="a">
    <p ng-include="b.html"></p>
</p>

app.js

var app = angular.module('App',[]);
//主模块
app.controller("a", function($scope) {
    $scope.name= "a_name";
});
//子模块
app.controller("b", function($scope) {
    $scope.name= "b_name";
});

b.html

<script>
    var $scope = angular.element("#c").scope();
    console.log($scope.name);//理论上应该返回b_name,实际返回a_name
</script>
<p ng-controller="b">
    <p id="c"></p>
</p>
某草草某草草2744 days ago635

reply all(1)I'll reply

  • 阿神

    阿神2017-05-15 16:57:20

    In fact, what you get is the parent scope. You want to get the name. In fact, you only need to distinguish the attribute values ​​f_name and c_name in the parent and child controllers

    reply
    0
  • Cancelreply