只有在页面中使用角度通过AJAX加载 [英] Use Angular only in page loaded via ajax

查看:60
本文介绍了只有在页面中使用角度通过AJAX加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<html>
<head>
  <title></title>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js"></script>
</head>
<body>
    <div id="ajax-content-here">

    </div>
</body>
</html>

我真的很新的角度,所以请温柔!

I am really new to angular so please be gentle!

这样的想法是在标题如上所述 - 与包括在头部角库我加载我的网页。后来我加载用ajax(jQuery的)数据(如下所示),在div容器中。

So the idea is as stated above in the title - I load my page with angular library included in head section. Later I load data (below) into the div container using ajax (jquery).

<div id="angular-test">
    <div>
      <label>Name:</label>
      <input type="text" ng-model="yourName" placeholder="Enter a name here">
      <hr>
      <h1>Hello {{yourName}}!</h1>
    </div>
</div>

我想以某种方式初始化角活跃仅在#角测试容器Ajax请求完成后。我认为它是与范围的对象吧?

I would like to somehow initialize the angular to be active only inside #angular-test container after ajax request is completed. I assume it has something to do with scope object right?

谢谢!

推荐答案

角做初始化(其中包括寻找NG-应用程序和模板编译)在DOM完成加载(在DOMContentLoaded事件)。角度没有办法知道的DOM被改变和一个纳克应用内被添加到后一次AJAX的响应接收到的DOM

Angular does initialization (which includes looking for ng-app and template compilation) once the DOM finishes loading (on DOMContentLoaded event). Angular does not have a way to know that the DOM is changed and an ng-app is added to the DOM later once the AJAX response is received.

所以,你可以尝试手动引导初始化后,DOM是真正完成(也就是你的jQuery AJAX回调,分配给的innerHTML后)。在回调做类似如下:

So, you can try initializing bootstrap manually after the DOM is really finalized (that is on your jquery ajax callback, after you assign to innerHTML). In the callback do something like the following:

  angular.bootstrap($('#ajax-content-here'));

假设在 Ajax的内容,这里的内容指一些 NG-应用
看看手册初始化部分在 http://docs.angularjs.org/guide/bootstrap

这篇关于只有在页面中使用角度通过AJAX加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆