http://jsfiddle.net/2dgzt18a/Angular Directive関数呼び出しで親モデルが更新されないのはなぜですか?
入力でEnterキーを押したときに、親モデルが更新されることを期待しています。しかし、それはしません。コンソールログからの出力は、それを実行する必要があるように有望です。 $ watchを使用する必要がありますか?それを疑うが、私は尋ねると思った。
HTML
<div data-ng-app="testApp">
<div data-ng-controller="testCtrl">
<strong>{{pkey}}</strong>
<span data-test-directive
data-parent-item="pkey"
data-parent-update="update(pkey)"></span>
</div>
</div>
DIRECTIVE
var testApp = angular.module('testApp', []);
testApp.directive('testDirective', function ($timeout) {
return {
scope: {
key: '=parentItem',
parentUpdate: '&'
},
replace: true,
template: '<div><input type="text"></input></div>',
link: function(scope, elem, attrs) {
elem.bind('keydown keypress', function(event) {
if (event.which === 13) {
scope.parentUpdate({ pkey: 'D+' + scope.key});
event.preventDefault();
}
})
}
};
});
CONTROLLER私はディレクティブのコントローラを使用して、この作品を見ていると信じていますが、
testApp.controller('testCtrl', function ($scope) {
$scope.pkey = 'golden';
$scope.update = function (k) {
// Expecting local variable k, or $scope.pkey to have been
// updated by calls in the directive's scope.
console.log('CTRL:', $scope.pkey, k);
$scope.pkey = 'C+' + k;
console.log('CTRL:', $scope.pkey);
};
});
私は、に興味があるので、 Keypressイベント、なぜ私はリンクを使用する必要があります。