【发布时间】:2019-08-24 18:06:57
【问题描述】:
Angularjs 何时触发双向数据绑定?
在 AngularJS 应用程序中,我有一个父指令和一个子指令。
父指令
angular
.module('myApp')
.directive('customForm', function(customService, apiV1, Constants, $timeout) {
return {
restrict: 'E',
scope: {
param1: '=',
param2: '=?',
boolean1: '@?'
},
template,
link: function(scope, parentController) {
scope.data = customService.newClient;
//some stuff...
childDirective JS:
angular
.module('myApp')
.directive('customToolForm', function () {
return {
restrict: 'E',
scope: {
name: '=',
city: '=',
postalCode: '='
},
template,
controller: function ($scope, $rootScope, Constants, apiV1, customService) {
$scope.doSomethingWithPostalCode = function() {
$scope.$parent.doSomethingWithPostalCode();
}
//some stuff...
parentDirective HTML 片段:
<address-client-creation name="data.client.name" city="data.client.city"
postal-code="data.client.postalCode">
</address-client-creation>
childDirective HTML 片段:
<input maxlength="5" type="text" data-ng-model="postalCode"
data-ng-change="doSomethingWithPostalCode();">
我的问题是:
从childDirective触发doSomethingWithPostalCode方法时,child中的postalCode的值与parent的client.postalCode不一样,而是在方法的末尾。
看来更新父值的双向绑定事件是在函数调用之后发生的
所以我的问题是在调用方法之前确保 $parent 范围更新的最佳方法是什么?
【问题讨论】:
标签: html angularjs angularjs-directive angularjs-scope