In this plunk私は、2回インスタンス化された指令を持っています。いずれの場合も、ディレクティブの結果(テンプレートによって定義されたもの)は正しく表示されます。指令で 'scope'と 'var'の間の関数を宣言するのはいつですか?
質問はgetValue2()
をscope.getValue2
またはvar getValue2
と定義する必要があるかどうかです。それぞれの命令を使用するタイミングは?
HTML
instance 1 = <div dirx varx="1"></div>
<br/>
instance 2 = <div dirx varx="2"></div>
Javascriptを
var app = angular.module('app', []);
app.controller('myCtl', function($scope) {
});
app.directive('dirx', function() {
var directive = {};
directive.restrict = 'EA';
directive.scope = {
varx: '='
};
directive.template = '{{getValue()}}';
directive.link = function (scope, element, attrs) {
scope.getValue = function(){
return getValue2();
};
var getValue2 = function() {
return scope.varx * 3;
}
};
return directive;
});