我正在学习Angular,我想知道是否有更好的方法将此指令链接到我的服务?
大多数情况下,我想知道在模型中存储Guid作为Id,然后在我的服务中引用该ID是链接指令和服务的好方法吗?或者是否有更“角度”的方式来做到这一点?
指令:
myApp.directive("myDialog", ['$compile', function ($compile) {
return {
restrict: 'E',
scope: {
model: "="
},
template: "<div id='dialog_{{model.dialogId}}' ng-include='model.templateUrl' style='display: none'></div>",
replace: true
}]);
服务:
myApp.service("myDialogSvc", function () {
// This will be called by the controller
this.init = function (template) {
var model = {};
model.template = template;
var id = newGuid(); // Gets a new Guid -- defined elsewhere
model.dialogId = id;
return model;
}
this.open = function (model) {
$("#dialog_" + model.dialogId).dialog({
modal: true
});
}
});
答案 0 :(得分:1)