调用另一个自定义指令的角度自定义指令
Angular custom directive calling another custom directive
我正在开发一个角度框架,用户可以在其中使用自定义指令配置页眉,菜单,页脚和选定页面。要完成此要求,我一度需要以下内容。我在网上看到过例子,但并没有很好地解释它。
要求是第一个自定义指令的 templateUrl 应替换为应调用另一个自定义指令的模板属性。
以下带有模板 URL 的代码工作正常。
angular.module("app",[]);
angular.module("app").controller("productController", ['$scope', function ($scope) {
}]);
angular.module("app").directive("tmHtml", function () {
return {
transclude: false,
scope: {
},
controller: "productController",
templateUrl: "/templates/HideShow.html"
};
});
但是,当我按如下方式更改上面的代码时。我正在进行更改,以便我的自定义指令 tmHtml 调用另一个自定义指令。
angular.module("app").directive("tmHtml", function () {
return {
transclude: false,
scope: {
},
controller: "productController",
template: ``<hideShow></hideShow>``
};
});
隐藏显示的新指令编写如下
angular.module("app").directive("hideShow", function () {
return {
tempateUrl: "/templates/HideShow.html"
};
});
它不起作用。我知道我在这里错过了一些东西。我找不到。感谢帮助
工作代码:
var app = angular.module('plunker', []);
app.controller('productController', function($scope) {
});
app.directive("hideShow", function() {
return {
templateUrl: "hideshow.html"
};
});
app.directive("tmHtml", function() {
return {
transclude: false,
scope: {},
controller: "productController",
template: "<hide-show></hide-show>"
};
});
问题在于hideShow
指令中templateUrl
的拼写。
演示 : http://plnkr.co/edit/TaznOeNQ7dM9lyFgqwCL?p=preview
尝试使用 controllerAs 定义控制器:
angular.module("app").directive("tmHtml", function () {
return {
transclude: false,
scope: {
},
controllerAs: "productController",
templateUrl: "/templates/HideShow.html"
};
});
angular.module("app").directive("tmHtml", function () {
return {
transclude: false,
scope: {
},
controller: "productController",
template: ``<hideShow></hideShow>``
};
});
必须替换为
angular.module("app").directive("tmHtml", function () {
return {
transclude: false,
scope: {
},
controller: "productController",
template: "<hide-show></hide-show>"
};
});
在属性 template
下,添加 Html。因此,您仍然必须在那里使用蛇形外壳,就像在 HTML 文件中一样
您的第一个指令可能具有您观察到的最终作用域属性。
然后它可能会包装第二个指令。如果需要,您的指令可以作为父母和孩子进行沟通。
相关文章:
- 如何在自定义指令中获取计算的属性
- 向动态生成的DOM添加Angular自定义指令
- 自定义指令中的AngularJS ng模型
- 如何在html中以角度显示自定义指令的作用域
- 自定义指令模板中的AngularJS控制变量
- AngularJS通过自定义指令传递点击的项目
- 如何在angularjs中将多个作用域变量传递到自定义指令中
- 处理角度自定义指令中的onReady事件
- 为什么ng中的自定义指令在循环完成后重复运行
- 对于使用传递的数据计算的局部范围变量,角度绑定在自定义指令中不起作用
- Angular.JS自定义指令;不起作用
- 如何通过Angular中的$resource发送来自自定义指令的输入值
- AngularJS:如何访问自定义指令中的input[date]min属性
- AngularJS:ngRepeat作用域在具有隔离作用域和ngTransclude的自定义指令中失败
- 如何使用其他自定义指令中的元素标记作为另一个自定义指令的模板
- 如何限制自定义指令的可能属性值
- 动态网格作为自定义指令
- AngularJS - 在自定义指令中继承ngModel
- 调用另一个自定义指令的角度自定义指令
- 在 AngularJs 中验证自定义指令(我想让它成为必需的)