<i id='fkosX'><tr id='fkosX'><dt id='fkosX'><q id='fkosX'><span id='fkosX'><b id='fkosX'><form id='fkosX'><ins id='fkosX'></ins><ul id='fkosX'></ul><sub id='fkosX'></sub></form><legend id='fkosX'></legend><bdo id='fkosX'><pre id='fkosX'><center id='fkosX'></center></pre></bdo></b><th id='fkosX'></th></span></q></dt></tr></i><div id='fkosX'><tfoot id='fkosX'></tfoot><dl id='fkosX'><fieldset id='fkosX'></fieldset></dl></div>
  • <small id='fkosX'></small><noframes id='fkosX'>

        <bdo id='fkosX'></bdo><ul id='fkosX'></ul>

        <legend id='fkosX'><style id='fkosX'><dir id='fkosX'><q id='fkosX'></q></dir></style></legend>

      1. <tfoot id='fkosX'></tfoot>
      2. 如何在 Angular 1.5 组件中等待绑定(没有 $scope.$watch)

        时间:2023-09-30
              <tbody id='x5119'></tbody>

              <bdo id='x5119'></bdo><ul id='x5119'></ul>
            • <tfoot id='x5119'></tfoot>

            • <legend id='x5119'><style id='x5119'><dir id='x5119'><q id='x5119'></q></dir></style></legend>
              <i id='x5119'><tr id='x5119'><dt id='x5119'><q id='x5119'><span id='x5119'><b id='x5119'><form id='x5119'><ins id='x5119'></ins><ul id='x5119'></ul><sub id='x5119'></sub></form><legend id='x5119'></legend><bdo id='x5119'><pre id='x5119'><center id='x5119'></center></pre></bdo></b><th id='x5119'></th></span></q></dt></tr></i><div id='x5119'><tfoot id='x5119'></tfoot><dl id='x5119'><fieldset id='x5119'></fieldset></dl></div>

              <small id='x5119'></small><noframes id='x5119'>

                1. 本文介绍了如何在 Angular 1.5 组件中等待绑定(没有 $scope.$watch)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在编写一个 Angular 1.5 指令,但我遇到了一个令人讨厌的问题,试图在绑定数据存在之前对其进行操作.

                  I'm writing an Angular 1.5 directive and I'm running into an obnoxious issue with trying to manipulate bound data before it exists.

                  这是我的代码:

                  app.component('formSelector', {
                    bindings: {
                      forms: '='
                    },
                    controller: function(FormSvc) {
                  
                      var ctrl = this
                      this.favorites = []
                  
                      FormSvc.GetFavorites()
                      .then(function(results) {
                      ctrl.favorites = results
                      for (var i = 0; i < ctrl.favorites.length; i++) {
                        for (var j = 0; j < ctrl.forms.length; j++) {
                            if (ctrl.favorites[i].id == ctrl.newForms[j].id) ctrl.forms[j].favorite = true
                        }
                       }
                      })
                  }
                  ...
                  

                  如您所见,我正在进行 AJAX 调用以获取收藏夹,然后对照我的绑定表单列表检查它.

                  As you can see, I'm making an AJAX call to get favorites and then checking it against my bound list of forms.

                  问题是,即使在绑定被填充之前,承诺就已经实现了......所以当我运行循环时, ctrl.forms 仍然是未定义的!

                  The problem is, the promise is being fulfilled even before the binding is populated... so that by the time I run the loop, ctrl.forms is still undefined!

                  如果不使用 $scope.$watch(这是 1.5 组件吸引力的一部分),我如何等待绑定完成?

                  Without using a $scope.$watch (which is part of the appeal of 1.5 components) how do I wait for the binding to be completed?

                  推荐答案

                  你可以使用新的生命周期钩子,特别是 $onChanges,通过调用isFirstChange<检测绑定的第一次变化/代码>方法.在此处了解更多信息.

                  You could use the new lifecycle hooks, specifically $onChanges, to detect the first change of a binding by calling the isFirstChange method. Read more about this here.

                  这是一个例子:

                  <div ng-app="app" ng-controller="MyCtrl as $ctrl">
                    <my-component binding="$ctrl.binding"></my-component>
                  </div>
                  
                  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.4/angular.js"></script>
                  <script>
                    angular
                      .module('app', [])
                      .controller('MyCtrl', function($timeout) {
                        $timeout(() => {
                          this.binding = 'first value';
                        }, 750);
                  
                        $timeout(() => {
                          this.binding = 'second value';
                        }, 1500);
                      })
                      .component('myComponent', {
                        bindings: {
                          binding: '<'
                        },
                        controller: function() {
                          // Use es6 destructuring to extract exactly what we need
                          this.$onChanges = function({binding}) {
                            if (angular.isDefined(binding)) {
                              console.log({
                                currentValue: binding.currentValue, 
                                isFirstChange: binding.isFirstChange()
                              });
                            }
                          }
                        }
                      });
                  </script>

                  这篇关于如何在 Angular 1.5 组件中等待绑定(没有 $scope.$watch)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                    <i id='i36qv'><tr id='i36qv'><dt id='i36qv'><q id='i36qv'><span id='i36qv'><b id='i36qv'><form id='i36qv'><ins id='i36qv'></ins><ul id='i36qv'></ul><sub id='i36qv'></sub></form><legend id='i36qv'></legend><bdo id='i36qv'><pre id='i36qv'><center id='i36qv'></center></pre></bdo></b><th id='i36qv'></th></span></q></dt></tr></i><div id='i36qv'><tfoot id='i36qv'></tfoot><dl id='i36qv'><fieldset id='i36qv'></fieldset></dl></div>
                    • <bdo id='i36qv'></bdo><ul id='i36qv'></ul>
                    • <small id='i36qv'></small><noframes id='i36qv'>

                      <legend id='i36qv'><style id='i36qv'><dir id='i36qv'><q id='i36qv'></q></dir></style></legend>

                          <tbody id='i36qv'></tbody>

                        <tfoot id='i36qv'></tfoot>