gpt4 book ai didi

Angularjs指令-指令- Controller -服务交互

转载 作者:行者123 更新时间:2023-12-02 05:02:24 25 4
gpt4 key购买 nike

标题有点啰嗦,但是我的现状是:

我有一个指令 (D1),它是我在根目录中的模态弹出窗口的纯标记。我有一个位于 Controller 范围 (C1) 中的指令 (D2)。我想让 D2 在模态 D1 中设置内容。我有一个注入(inject)到 C1 的服务 (S1),它从我希望最终注入(inject)到 D1 的网络中提取数据。

D1 如下(模式的简单标记): enter image description here

C1中的S1返回数据给D2填充D1后,D1会是这样的。 D2 将负责定义 D1 中的模型数据,例如 {{title}}。 enter image description here

我们的想法是拥有一个通用的、解耦的模态 (D1),可以通过另一个指令 (D2) 定义/填充它。

我很难实现这个功能。奖励积分,我想让 D1 有一个 API,我可以调用它来用不同的元素(例如复选框、输入、文本)填充模态。这与仅在 D1 外部构建标记然后将其完全注入(inject) D1 不同。

代码基础是:

myApp.controller('C1', function(S1){
var results = S1.query();
// populate D1 attributes with results from S1? or a getter function in the controller to returns results to D1.
}

myApp.directive('D1', function() {
var createCheckBox = function( name ){ // one of many different element available exposed through D1's API.
var checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.name = name;
return checkbox;
}
return{
restrict: "E",
templateUrl: '/path/to/my/modal.html',
controller: function( $scope ){ // hopefully this would be D1's API
$scope.modalContent = [];
this.addCheckBox = function( checkboxName ){
$scope.push( this.createCheckBox( checkboxName ) );
}
}
link: function( scope, element, attrs ){
// set up events on the modal (such as closing it)
}
}
} // end of D1

myApp.directive('D2', function() {
return{
restrict: "A",
require: 'D1', // <-- do not think this is possible with my setup.
link: function( scope, element, attrs ){
element.bind( 'click', function(){
// loop through data returned by service
D1.addCheckBox(/* checkbox's name */ );
// end loop
});

}
}
}// end of D2

HTML 标记

<div ng-controller="root">
<div ng-controller="C1">
<span D2></span>
</div>
<D1></D1>
</div>

感谢您的关注!

TL;DR:寻找另一个问题来帮助解决

最佳答案

哇好长的问题!不确定我是否完全理解,但听了 Jonnybojandles 的评论...

你可以 $broadcast$on 吗?

$rootScope.$broadcast('MyEvent'); // Fire an event

$scope.$on('MyEvent', function () { }); // Handle the event

还是简单地使用$rootScope

关于Angularjs指令-指令- Controller -服务交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15144088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com