スコープがディレクティブに継承されているが、分離されていない場合は、ng-clickを動作させることができます。ここでUPDATE: The point is that I want the click function to be defined as part of the directive... moving the function definition into a different scope is not what I want.
スコープが分離されたディレクティブにng-clickを使用するにはどうすればよいですか?
は継承された範囲での作業例です: はhttps://codepen.io/anon/pen/PGBQvj
ここで分離されたスコープで壊れた例です。 https://codepen.io/anon/pen/jrpkjp
(数字をクリックして、彼らは最初の例ではなく、第二にインクリメント)
いくつかのコード...
HTML
<div ng-app="myApp" ng-controller="baseController">
<my-directive ng-click="hello()" current="current"></my-directive>
</div>
ディレクティブ継承されたスコープを持ちます:
angular.module('myApp', [])
.controller('baseController', function($scope) {
$scope.current = 1;
})
.directive('myDirective', function() {
return {
link: function(scope, element, attrs) {
scope.hello = function() {
scope.current++
};
},
replace: true,
scope: true,
template: '<div><child> <strong>{{ current }}</strong></child></div>'
}
})
.directive('child', function() {
return {
link: function(scope, element, attrs) {
console.log("horeee");
}
}
});
分離された範囲と同じディレクティブ:孤立ディレクティブで
angular.module('myApp', [])
.controller('baseController', function($scope) {
$scope.current = 1;
})
.directive('myDirective', function() {
return {
link: function(scope, element, attrs) {
scope.hello = function() {
scope.current++
};
},
replace: true,
scope: {
current:'='
},
template: '<div><child> <strong>{{ current }}</strong></child></div>'
}
})
.directive('child', function() {
return {
link: function(scope, element, attrs) {
console.log("horeee");
}
}
});
ペンで試してみましたか?動作しません。 – geoidesic
申し訳ありませんが、私はあなたの質問を誤解しています。あなたのコントローラのスコープに 'hello()'を定義するとうまくいくはずです。@ geoidesic – Zen
これはうまくいきません。提案する前に試してください:) tx。 – geoidesic