Javascript 指令AngularJs中的触发器控制器范围
我有一个Javascript 指令AngularJs中的触发器控制器范围,javascript,angularjs,angularjs-directive,angularjs-scope,Javascript,Angularjs,Angularjs Directive,Angularjs Scope,我有一个TimelineController,在作用域上有一个publish功能,它将向服务器发送一些数据 我的时间线由2个指令组成 时间线(元素) 共享职位(要素) 我希望能够从share post指令调用我的TimelineController上的publish功能,这可能吗 控制器 function TimelineController($scope,TimelineService) { $scope.posts = []; $scope.pu
TimelineController
,在作用域上有一个publish
功能,它将向服务器发送一些数据
我的时间线由2个指令组成
- 时间线(元素)
- 共享职位(要素)
share post
指令调用我的TimelineController
上的publish
功能,这可能吗
控制器
function TimelineController($scope,TimelineService)
{
$scope.posts = [];
$scope.publish = function(wall_type,wall_id,text,video,image) {
console.log('click controller');
TimelineService.publishPost(wall_type,wall_id,text,video,image).$promise.then(function(result){
$scope.posts.push(result.response);
});
};
}
时间线指令:
function timelineDirective() {
return {
restrict: 'E',
replace: true,
scope: {
type: '@',
ids: '@',
postime: '&',
posts: '='
},
templateUrl:"/js/templates/timeline/post-tmpl.html",
controller: function($scope,$element) {
this.type = $element.attr('type');
this.ids = $element.attr('ids');
}
}
};
时间线指令模板
<ol class="timeline" ng-init="postime({wall_type:type,wall_id:ids})">
<li>
<share-post></share-post>
</li>
<li ng-repeat="post in posts">
{{post.text}}
</li>
</ol>
<div class="module comment">
<div class="content">
<textarea class="form-control" ng-model="text" placeholder="What is going on..." rows="2"></textarea>
</div>
<button type="button" class="btn" ng-click="pub()"> Share</button>
</div>
Sharepost指令模板
<ol class="timeline" ng-init="postime({wall_type:type,wall_id:ids})">
<li>
<share-post></share-post>
</li>
<li ng-repeat="post in posts">
{{post.text}}
</li>
</ol>
<div class="module comment">
<div class="content">
<textarea class="form-control" ng-model="text" placeholder="What is going on..." rows="2"></textarea>
</div>
<button type="button" class="btn" ng-click="pub()"> Share</button>
</div>
分享
更改指令,使其在作用域中有一个额外的项,如下所示onPublish:“@”
然后在html中,您可以像这样传递指向要调用的控制器函数的指针:
<share-post on-publish="publish"></share-post>
您使用您的指令只是绑定来自控制器的事件单击,类似于:
angular.module('module').directive('sharePost', [
function(){
return {
link: function (scope, element, attr) {
var clickAction = attr.clickAction;
element.bind('click',function (event) {
scope.$eval(clickAction);
});
}
};
}]);
html
<a sharePost click-action="publish(wall_type,wall_id,text,video,image)"> publish</a>
发布
我认为您应该直接将TimelineService服务注入指令,或者使用Attributes将“发布”功能注入指令的范围。我希望避免将服务注入指令directive@Fabrizio,让我说清楚,是否要从指令触发控制器上的单击事件?@jack.the.ripper我要从指令中的事件触发控制器中的函数$scope.publish
。这种方式不起作用,因为如果我传递publish
方法,您如何查看share post
指令是timeline
的子指令,我想您是如何显示的,尝试获取父指令publish
的。我错过了嵌套指令,通过将函数从控制器传递到父指令,然后从父指令传递到子指令,可以实现对控制器方法的调用。但这有点恶心。另一种方法是在child指令和controller中注入一个服务(一个新的服务),这样你就可以用它来交流。如果你解释了我说的话,那就比我好,从父指令传递到子指令是很讨厌的。非常感谢,您可以展示一个小示例,说明我如何将服务放入通信、指令和控制器中吗?现在我认为,在指令中单击一次后声明要调用的方法是正确的,只要相应地命名属性,例如,点击后点击的或类似的东西可以,但我认为这种方式不是更正确的方式:因为我在时间线指令上传递publish
范围,它不需要它,只是一个桥梁,让范围可用于子指令。如果我错了,请纠正我,如果这个解决方案很好谢谢你的回答,它看起来很有趣,但我不知道函数
来自哪里,该函数的意思是控制器上的发布
函数?谢谢,我刚刚从你的代码中复制并粘贴了它,忘了引用控制器作用域,那只是你的发布函数好吧,现在说得更有意义,我会尝试让它听起来不错:)我尝试了这个,但它不起作用,因为指令仍然不知道publish
函数。该sharePost
指令是另一个指令的子指令