由于某种原因,我的指令中的链接函数没有被调用。我可以看到我的指令是用console.log
而不是链接函数调用的。也不介意我将使用我的父指令的控制器参数。我也试过限制:'E'也没有运气。我没有在这个例子中使用它。不知道是什么导致它跳过链接。有什么想法吗?
module FormTest {
angular
.module('FormTest') //Gets the FormTest Module
.directive('jiTab', function () {
console.log('directive was hit');
function linkFn(scope, ele, attrs, controller) {
console.log('Link is called');
};
return {
require: '^ji-Tabset',
restrict: 'C',
transclude: true,
link: linkFn
}
});
}
HTML
<ji-form name="Main Form">
<ji-tabset name="Tabs">
<ji-tab tab-name="General"></ji-tab>
<ji-tab tab-name="Stats"></ji-tab>
</ji-tabset>
</ji-form>
家长指示
module FormTest {
angular
.module('FormTest') //Gets the FormTest Module
.directive('jiTabset', function () {
return {
restrict: 'E',
transclude: true,
replace: true,
templateUrl: 'FormTest/views/ji-Tabset.html',
controller: function ($scope) {
var tabPanelItems = $scope.tabPanelItems = [];
$scope.tabSettings = {
dataSource: tabPanelItems
}
}
};
});
}
答案 0 :(得分:4)
module FormTest {
angular
.module('FormTest') //Gets the FormTest Module
.directive('jiTab', function () {
console.log('directive was hit');
function linkFn(scope, ele, attrs, controller) {
console.log('Link is called');
};
return {
require: '^ji-Tabset', //<-- this must be `^jiTabset` read mistake 1
restrict: 'C', //<-- this must be `E` which stands for element, (jiTab) C is for class, read mistake 2
transclude: true,
link: linkFn
}
});
}
来自docs
规范化 Angular规范化元素的标记和属性名称,以确定哪些元素与哪些指令匹配。我们通常通过其区分大小写的camelCase规范化名称(例如ngModel)来引用指令。但是,由于HTML不区分大小写,我们通过小写形式引用DOM中的指令,通常使用DOM元素上的划线分隔属性(例如ng-model)。
规范化过程如下:
restrict选项通常设置为:
&#39; AEC&#39; - 匹配属性或元素或类名
您的ng-transclude
指令中没有jiTabset
属性,请确保您拥有'FormTest/views/ji-Tabset.html'
打开浏览器控制台
angular.module('FormTest', []);
angular.module('FormTest') //Gets the FormTest Module
.directive('jiTabset', function () {
return {
restrict: 'E',
transclude: true,
replace: true,
template: '<div>ji-tabset<div ng-transclude></div></div>',
controller: function ($scope) {
var tabPanelItems = $scope.tabPanelItems = [];
$scope.tabSettings = {
dataSource: tabPanelItems
}
}
};
});
angular.module('FormTest') //Gets the FormTest Module
.directive('jiTab', function () {
function linkFn(scope, ele, attrs, controller) {
console.log('Link is called');
};
return {
require: '^jiTabset',
restrict: 'E',
transclude: true,
link: linkFn
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.js"></script>
<div ng-app="FormTest">
<ji-form name="Main Form">
<ji-tabset name="Tabs">
<ji-tab tab-name="General"></ji-tab>
<ji-tab tab-name="Stats"></ji-tab>
</ji-tabset>
</ji-form>
</div>
&#13;
答案 1 :(得分:1)
通过将变量赋值给函数并通过变量名称调用它,
angular
.module('FormTest') //Gets the FormTest Module
.directive('jiTab', function () {
var linkFn = function (scope, ele, attrs, controller) {
console.log('Link is called');
};
return {
restrict: 'C',
transclude: true,
link: linkFn
}
});
通过这种方式,我认为你可以实现我希望。
答案 2 :(得分:1)
链接功能是否必须在指令ITSELF之外生效?
试试这个:
module FormTest {
angular
.module('FormTest') //Gets the FormTest Module
.directive('jiTab', function () {
console.log('directive was hit');
return {
require: '^jiTabset',
restrict: 'E',
transclude: true,
link: linkFn
}
});
function linkFn(scope, ele, attrs, controller) {
console.log('Link is called');
};
}
答案 3 :(得分:1)
当一个指令使用这个选项(
require
)时,$ compile将抛出一个 除非找到指定的控制器,否则出错。 ^前缀表示 该指令在其父项上搜索控制器 (没有^前缀,指令将查找控制器 只是它自己的元素。)
更新你的指令:
.directive('jiTab', function () {
console.log('directive was hit');
function linkFn(scope, ele, attrs) {
console.log('Link is called');
};
return {
restrict: 'E',
transclude: true,
link: linkFn
}
});