Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 在Angluarjs中处理未准备好的绑定_Angularjs_Angularjs Components - Fatal编程技术网

Angularjs 在Angluarjs中处理未准备好的绑定

Angularjs 在Angluarjs中处理未准备好的绑定,angularjs,angularjs-components,Angularjs,Angularjs Components,我将我的组件作为绑定传递一个对象 .component('selectionButton', { bindings: { parentForm : '<' }, templateUrl: 'selection-button-component.html', controller: 'selectionButtonController', controllerAs: 'selBtnCtrl' }); 在我的控制器中,我在一个函数中调用

我将我的组件作为绑定传递一个对象

.component('selectionButton', {
    bindings: {
        parentForm : '<'
    },
    templateUrl: 'selection-button-component.html',
    controller: 'selectionButtonController',
    controllerAs: 'selBtnCtrl'
});
在我的控制器中,我在一个函数中调用vm.parentForm,当我在页面完全加载后单击按钮时触发该函数,但我总是将其视为未定义,即使我使用$onChanges更改了它的值

当我检查vm.$onChanges函数时,我可以看到vm.parentForm的值正在changesObj.parentForm中获得新值

我怎样才能解决这个问题

编辑: 我尝试用ng来包装我的元素,如下所示:

<span ng-if="fullPage.posteForm">
            <selection-button parent-form="fullPage.posteForm" ></selection-button>
        </span>

但这不起作用。我还尝试了双向绑定,但效果不佳。

当您将对象传递给组件时,它可能没有准备好,因为您正在等待承诺或其他操作得到解决,但对象定义未定义。对于那种你可以使用的escenario。 下面是一个说明这种情况的工作示例。打开控制台,查看绑定在超时后何时更新


注意:如果组件模板中使用的对象名称等于传递给组件的名称,则不需要创建新对象,它将在组件中更新,并在模板中可用。

两种可能的解决方案。1.将绑定更改为=。2.使用ng if=vm.parentForm包装html,因此一旦初始化,html将重新编译。您根本不需要使用ng if来解决此问题:在子组件内使用onChanges,这样每次更新值时,子组件内都会触发hook onChanges,因此您可以分配新的对象值。onChanges=functionchanges{if changes.parentForm.currentValue!==undefined{this.parentForm=changes.parentForm.currentValue;}实际上,您不必更新组件的本地作用域,因为它会在任何绑定更改时自动更新。您可以共享其余的控制器代码吗?也许问题根本不在绑定中。make fiddle/plunk-这里的所有功能都可以正常工作
<span ng-if="fullPage.posteForm">
            <selection-button parent-form="fullPage.posteForm" ></selection-button>
        </span>