Javascript 绑定手表和$timeout顺序

Javascript 绑定手表和$timeout顺序,javascript,angularjs,Javascript,Angularjs,我遇到了一个设置HTML的组件的代码:$scope.htmlContent=$sce.trustAsHtml(content),然后,在$timeout中调用一个函数,该函数将使用$element.find('.stuff')… 该代码位于$onInit函数的$scope.$watch内 我的问题是:我们是否100%确定内容的DOM元素将在$element.find之前呈现?有更好的方法吗 需要理解的最低代码: (function() { 'use strict'; angular.mod

我遇到了一个设置HTML的组件的代码:
$scope.htmlContent=$sce.trustAsHtml(content)
,然后,在
$timeout
中调用一个函数,该函数将使用
$element.find('.stuff')…

该代码位于
$onInit
函数的
$scope.$watch

我的问题是:我们是否100%确定内容的DOM元素将在
$element.find之前呈现?有更好的方法吗

需要理解的最低代码:

(function() {
'use strict';

  angular.module('mymodule').component('exampleComponent', {
    templateUrl: 'path/template.html',
    bindings: { content: '<' },
    controller: ExampleComponentCtrl
  });

  function ExampleComponentCtrl($element, $sce, $scope, $timeout) {
    this.$onInit = function() {
      $scope.$watch(function() {
        return $sce.valueOf(self.content);
      }, function() {
        // irrevelant stuff that creates variable content that contains html code

        $scope.htmlContent = $sce.trustAsHtml(content);

        // use $timeout to wait for the inner HTML to be injected
        // so that we can find `.file-link` elements
        $timeout(function() { $element.find('.stuff').each... });
      });
    };
  }
)();
(函数(){
"严格使用",;
angular.module('mymodule').component('exampleComponent'{
templateUrl:'path/template.html',

绑定:{content:'不确定此组件实际应该实现什么…请查看以下内容:

function ExampleComponentCtrl($element, $scope, $compile) {
    var vm = this;

    vm.$onInit = function() {
    };

    vm.$onChanges = function() {
      if (vm.content) {
        $element.empty();
        $element.append($compile(vm.content)($scope)); // if you html does not contain angular, compile is not required
        console.log('Found element: ' + $element.find('p')[0].id);
      }
    };
}

你能分享完整的代码吗?@GauravSrivastava几乎完整的代码也许你可以使用带有
compile
函数的指令,访问控制器
$onInit
中的DOM?我认为@LeonardoChaia是对的,但我相信你的解决方案也很好,因为在编译后的下一个摘要中将触发超时离子。
function ExampleComponentCtrl($element, $scope, $compile) {
    var vm = this;

    vm.$onInit = function() {
    };

    vm.$onChanges = function() {
      if (vm.content) {
        $element.empty();
        $element.append($compile(vm.content)($scope)); // if you html does not contain angular, compile is not required
        console.log('Found element: ' + $element.find('p')[0].id);
      }
    };
}