【问题标题】:trigger Controller scope from directive AngularJs从指令 AngularJs 触发控制器范围
【发布时间】:2014-10-29 14:34:57
【问题描述】:

我有一个TimelineController,在作用域上有一个publish 函数,它将向服务器发送一些数据。

我的时间线由2个directives组成

  • 时间线(元素)
  • 分享帖子(元素)

我希望能够从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>

SharePost 指令:我想通过这个指令调用 TimelineController 上的发布

function sharePost() {

        return {
            restrict: 'E',
            replace: true,
            require: "^timeline",
            templateUrl:"/js/templates/timeline/share-tmpl.html",
            link: function($scope,$element,$attr,ctrl) {
                $scope.pub = function() {

                    // This does not work because it call the parent directive
                    // Instead of controller
                    $scope.publish(ctrl.type, ctrl.ids, $scope.text);
                }
            }
        }
    };

分享发布指令模板

<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>

【问题讨论】:

  • 我认为您应该将 TimelineService 服务直接注入您的指令中,或者使用属性将“发布”功能注入您的指令范围
  • 我想避免将服务注入指令
  • @Fabrizio,让我直说吧,你想从指令触发控制器上的点击事件吗?
  • @jack.the.ripper 我想从指令中的事件click 触发控制器中的函数$scope.publish

标签: javascript angularjs angularjs-directive angularjs-scope


【解决方案1】:

您使用指令只是绑定来自控制器的事件点击,例如:

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>

【讨论】:

  • 感谢您的回答,这似乎很有趣,但我不知道function 来自哪里,该功能是指控制器上的publish 功能?谢谢
  • 对不起,我只是从您的代码中复制并粘贴了它,忘记引用控制器范围,这只是您的发布功能
  • 好的,现在更有意义了,我会试试这听起来不错:)
  • 我试过了,但它不起作用,因为指令仍然不知道 publish 函数。 sharePost 指令是另一个指令的子指令
【解决方案2】:

更改您的指令以在onPublish: "@" 之类的范围内添加一个额外的项目,然后在您的 html 中,您可以将指针传递给您想要调用的控制器函数,如下所示:

 <share-post on-publish="publish"></share-post>

从你必须做的指令中调用它:

$scope.onPublish()(ctrl.type, ctrl.ids, $scope.text)

【讨论】:

  • 这种方式行不通,因为如果我通过publish 方法,您如何看到share-post 指令是timeline 的子项,您如何显示它尝试获取父指令@987654328 @我猜。
  • 我错过了嵌套指令,您可以通过将函数从控制器传递给父指令,然后从那里传递给子指令来实现调用控制器方法。但这有点恶心。另一种方法是在子指令和控制器中注入一个服务(一个新的),这样你就可以使用它来通信
  • 是的,你解释了我说的比我好,从父指令传递到子指令是令人讨厌的。非常感谢,您能否举一个小例子说明我如何使用服务进行通信、指令和控制器?
  • 现在我认为,一旦发生指令中的点击,声明你想要调用的方法是没有错的,只要相应地命名属性,例如on-post-clicked 或类似的东西
  • 没关系,但我认为这种方式不是更正确的方式:pastebin.com/Ytjbjq9x 因为我在 timeline 指令上传递了 publish 范围,它不需要它但是只是让scope 可用于子指令的桥梁。如果我错了,请纠正我,如果这个解决方案很好
猜你喜欢
  • 2013-05-28
  • 2016-03-28
  • 2018-08-30
  • 2014-07-13
  • 1970-01-01
  • 1970-01-01
  • 2015-01-11
  • 2015-04-30
  • 1970-01-01
相关资源
最近更新 更多