2
私は配列を受け付けるディレクティブを持っています。最初は未定義であり、ある時点で非同期的に定義されます。
ディレクティブは、これが正常に動作します
function XYZDirective() {
return {
restrict: 'EA',
replace: true,
scope: {
array: '='
},
controller: ['$scope', $scope => {
$scope.$watch(() => $scope.array, (newValue, oldValue) => {
$scope.firstElement = (newValue && newValue.length > 0)
? newValue[0]
: null;
});
}],
templateUrl: URL
};
}
に似ています。私はその後、$watch
を取り除き、単純なバインディングを使用したかったのです。
function XYZDirective() {
return {
restrict: 'EA',
replace: true,
scope: {
array: '='
},
controller: ['$scope', $scope => {
$scope.getFirstElement = function() {
return ($scope.array && $scope.array.length > 0) ? $scope.array[0] : null;
}
$scope.firstElement = $scope.getFirstElement();
}],
templateUrl: URL
};
}
私は$scope.firstElement
は毎回$scope.array
変更を再評価を受けることを期待します。
ただし、これは機能しません。私は$scope.array
にウォッチャを追加した場合、私はそれが更新さ見ることができますが、変更は$scope.firstElement