如何将angular.js数据绑定到导入的html?

时间:2019-03-20 18:30:27

标签: javascript jquery html angularjs ng-bind

我已将外部本地html文件(index2.html)导入到html文件的div(index.html,#container)中。我想使用angular将数据值绑定到导入的html文件中的某些标签。 到目前为止,这是我的代码-http://plnkr.co/edit/APXdpQzRUOfGehqSAJ9l?p=preview

index.html

<div id="container" ng-app="myApp" ng-controller="myController"></div>

<script type="text/javascript">
  $(function() {
    $("#container").load("index2.html");
  });

  var app = angular.module('myApp', []);
  app.controller('myController', function($scope) {
    $scope.name = "test";
  });     
</script>

index2.html

<div id="container2">
 <p>test content:</p>
 <p>{{name}}</p>
</div>

但是,它只是显示为文本{{name}}-而不是“ test”。我尝试将“ ng-app”和“ ng-controller”放置在#container2中,但还是一样。

1 个答案:

答案 0 :(得分:0)

与AngularJS和jQuery混合正在麻烦!

我找到了答案-我使用ng-include导入html而不是load函数。

<div id="container" ng-include="'index2.html'" ng-app="myApp" ng- 
controller="myController"></div>