简体   繁体   English

内容按角度动态加载时初始化ng-controller

[英]init ng-controller when content is loaded dynamically in angular

I'm learning Angular I tried to init a controller after create a new content by ajax (with jQuery, maybe it's not a good idea but just I'm starting to learning step by step.) 我正在学习Angular,我尝试通过ajax创建新内容后尝试初始化一个控制器(使用jQuery,也许这不是一个好主意,但我正在逐步学习。)

this is my code. 这是我的代码。

angular.module('app').controller('TestController',function($scope){
   $scope.products = [{
      'code': 'A-1',
      'name': 'Product 01'
   }];

   $scope.clickTest = function(){
      alert('test');
   }
});

$.ajax({..})
.done(function(html){   
  angular.element(document).injector().invoke(function($compile){
    $('#content').html(html);
    var scope =  $('#content').scope();
    $compile($('#content').contents())(scope);

  });
});

In my html I use ng-repeat but not load nothing, however when I click in ng-click="clickTest" it works! 在我的html中,我使用ng-repeat,但不加载任何内容,但是当我单击ng-click =“ clickTest”时,它将起作用! and ng-repeat is loaded. 并且ng-repeat已加载。 this is my problem I need that the ng-repeat load when I load for first time. 这是我的问题,我第一次加载时需要ng-repeat加载。

Thanks. 谢谢。

Sorry for my bad english. 对不起,我的英语不好。

with jQuery, maybe it's not a good idea 使用jQuery,也许这不是一个好主意

Yes spot on 是的


Now getting into your issue:- When you click on element with ng-click on the html, it works because it then runs a digest cycle and refreshes the DOM and your repeat renders. 现在进入您的问题:-当您用ng-click单击元素并在html上ng-click时,它会起作用,因为它随后会运行摘要循环并刷新DOM和重复渲染。 $Compile has already instantiated the controller and methods are available and attached to DOM. $Compile已经实例化了控制器,并且方法可用并且已附加到DOM。 But there is one digest cycle that runs in angular after controller initialization, which renders data in DOM, and that does not happen in your case since you are outside angular. 但是,在控制器初始化之后,有一个摘要循环以角度运行,它以DOM形式呈现数据,由于您不在角度范围内,因此这种情况不会发生。

You could do a scope.$digest() after compile to make sure element is rendered and digest cycle is run. 您可以在编译后执行scope.$digest()以确保呈现元素并运行摘要循环。

Also probably one more better thing would be to wrap it inside angularRootElement.ready function, just to make sure before the injector is accessed the element is ready, in your case enclosing it inside ajax callback saves you (the time for it to be ready) but better to have it. 还有可能更好的方法是将其包装在angularRootElement.ready函数中,以确保在访问注入器之前该元素已准备好,在您的情况下,将其包含在ajax回调中可以节省您(准备就绪的时间)但最好拥有它。

Try:- 尝试:-

$.ajax({..})
.done(function(html){  
    var rootElement = angular.element(document);
      rootElement.ready(function(){
        rootElement.injector().invoke(function($compile){
          var $content = $('#content'),
              scope =  $content.scope();

          $content.html(html);
          $compile($content.contents())(scope);
          scope.$digest();
        });
      });
});

Sample Demo 样本演示

Demo - Controller on dynamic html 演示-动态HTML上的控制器

Instead of getting html from the server you could create partials and templates, use routing , use angular ajax wrapper with $http etc... Well i would not suggest doing this method though - however based on your question you understand that already. 除了可以从服务器获取html之外,您还可以创建部分和模板,使用路由,将$ ajax角形ajax包装器与$ http等一起使用。。。但是我不建议这样做-但是根据您的问题您已经了解了。 There was a similar question that i answered last day 我昨天回答了一个类似的问题

If you are not looking for a better way to do what you are doing, ignore the following. 如果您不是在寻找一种更好的方式来做自己正在做的事情,请忽略以下内容。

The beauty of using frameworks like AngularJS and KnockoutJS (or many more) is that we don't have to worry about the timing of when the data loads. 使用诸如AngularJS和KnockoutJS(或更多)之类的框架的好处在于,我们不必担心数据加载的时间安排。 You just set up the bindings between the controller and the UI and once the data is loaded into the respective properties, these frameworks will take care of updating the UI for you. 您只需设置控制器和UI之间的绑定,然后将数据加载到各自的属性中,这些框架将为您更新UI。

I am not sure why you are trying to set up the UI using JQuery and waiting for the data to loaded first to do so but normally you will not have to do all that. 我不确定为什么要尝试使用JQuery设置UI并等待首先加载数据来这样做,但是通常不必这样做。

You can create a UI with ng-repeat and click bindings ,etc and make an ajax call to get the data from anywhere. 您可以使用ng-repeat创建UI,然后单击bindings等,并进行ajax调用以从任何地方获取数据。 Once the data is loaded, in the callback, just push the necessary data into the collection bound to the ng-repeat directive. 加载数据后,在回调中,只需将必要的数据推送到绑定到ng-repeat指令的集合中即可。 That is all you will have to do. 那就是您要做的全部。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM