简体   繁体   English

如何将angularJs用于动态创建的HTML(未加载到DOM)

[英]How to use angularJs for dynamically created HTML(Which is not loaded to DOM)

Hello I am very new to AngularJs. 您好,我是AngularJs的新手。 I am retrieving the HTML data from C# by calling ajax function. 我通过调用ajax函数从C#中检索HTML数据。 Here i am trying to use the AngularJs functionality for the dynamically generated HTML. 在这里,我试图将AngularJs功能用于动态生成的HTML。 But i am not able to see any AngularJs effect on this.But AngularJs effect is working fine for static HTML.Please help me on this. 但是我看不到任何AngularJs效果。但是AngularJs效果对于静态HTML来说效果很好。请帮助我。

Below is the example. 以下是示例。

C# Code: C#代码:

[WebMethod]
public static string DynamicHtml()
{
  StringBuilder sb=new StringBuilder();
  sb.append("    
                       <div ng-app='myApp' ng-controller='myCtrl'>
                           <h1 id='myName' ng-click='changeName()'>
                              {{firstname}}
                           </h1>
                        </div>
           "); 

  return sb.ToString();
}

HTML Code: myPage.aspx HTML代码: myPage.aspx

<html>
  <head>

      <script    src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
    </script>
  </head>
  <body>
    <div id="div1">
    </div>
  </body>
</html>

Script 脚本

$(document).ready({

     $.ajax({
          type:'POST',
          content-type:'application/json;charset-4',
          url:'myPage.aspx/DynamicHtml' 
          success:function(data){

               $("#div1").html(data.d);

               ChangeNameFunc();
                 } 
          })


        function ChangeNameFunc()
        {
            var app = angular.module('myApp', []);
            app.controller('myCtrl', function ($scope) {
            $scope.firstname = "John";
            $scope.changeName = function () {
            $scope.firstname = "Nelly";
        }
    });
  }

});

In the above code i am retrieving the html data from c# by using ajax call and binding it to div1 . 在上面的代码中,我通过使用ajax调用从c#中检索html数据并将其绑定到div1 When i try to click on John the name is not changing to Nelly. 当我尝试单击John时,名称未更改为Nelly。 Please note that Angular function worked when used in static page. 请注意,在静态页面中使用Angular函数时可以使用。

<html>
  <head>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
    </script>
  </head>
  <body ng-app='myApp' ng-controller='myCtrl'>
    <div id="div1">
    </div>
  </body>
</html>

Please try declaring the ng-app and ng-controller in the itself and let me know. 请尝试声明本身的ng-appng-controller ,让我知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 CSharpScript 与 DLL 中的“globals”类型一起使用,该 DLL 动态加载到自定义可收集的 AssemblyLoadContext 中 - How to use CSharpScript with 'globals' type from a DLL which is dynamically loaded into custom collectible AssemblyLoadContext 如何将由AngularJs动态生成的HTML页面导出为PDF? - How do I export my HTML page which is dynamically generated by AngularJs as PDF? 如何删除动态创建的事件处理程序? - How to remove event handler which is dynamically created? 覆盖动态创建和加载的程序集 - Override dynamically created and loaded assembly 如何动态触发动态创建的文本框的事件? - How can I dynamically fire an event for a textbox which is dynamically created? 无法决定对动态创建的行使用哪个数据控件 - Not able to decide which data control to use for dynamically created rows 如何在MEF中使用动态创建的对象? - How to use dynamically created object in MEF? 如何使用动态创建的按钮和文本框 - How to use buttons and textbox created dynamically 如何使用Java脚本动态创建按钮 - How to use java script for dynamically created button 如何根据正在执行的操作方法动态确定要使用哪个HTML“包装器”文件? - How to dynamically determine which HTML 'wrapper' file to use based on the Action Method being executed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM