0

我有一個TimelineController,它在作用域上有一個publish函數,它將向服務器發送一些數據。從指令AngularJs觸發控制器範圍

我的時間表是由2 directives

  • 時間表(元)
  • 股份後(元素)組成

我希望能夠從​​指令的publish調用功能在我的TimelineController是可能的嗎?

控制器

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); 
       } 
      } 
     } 
    }; 

Sharepost指令模板

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

我想你應該直接注入TimelineService服務到你的指令或在使用屬性將「發佈」功能添加到您的指令範圍中 – Neozaru 2014-10-29 14:41:36

+0

我想避免將服務注入到指令 – Fabrizio 2014-10-29 14:54:38

+0

@Fabrizio,讓我直觀一點,您是否想從指令中觸發控制器上的單擊事件? – 2014-10-29 14:59:29

回答

0

發佈更改您的指令有這樣onPublish: "@"範圍的額外項目然後在你的html中,你可以傳遞一個指向你想調用的控制器函數的指針,如下所示:

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

從指令調用這個你要做的:

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

這種方式是行不通的,因爲如果我通過'publish'方法,你如何看到'share-post'指令是'timeline'的子節點,你如何顯示它,試圖獲得父指令'publish'我猜。 – Fabrizio 2014-10-29 14:53:26

+0

我錯過了嵌套指令,您可以通過將控制器的函數傳遞給父指令,然後從那裏傳遞給子控件來實現調用控制器方法。但是這有點令人討厭。 另一種方法是在子指令和控制器中注入一個服務(一個新服務),這樣你就可以使用它來溝通 – 2014-10-29 15:08:52

+0

Yhea你解釋了我說的更好,然後我說,從父指令指向子指令。非常感謝,你可以舉一個小例子來介紹如何使用我可以加入溝通,指令和控制器的服務? – Fabrizio 2014-10-29 15:11:28

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

感謝你的回答,它似乎很有趣,但是我沒有得到'function'來自哪裏,那個函數就是控制器上的'publish'函數?謝謝 – Fabrizio 2014-10-29 15:56:40

+0

對不起,我只是從你的代碼複製並粘貼它,忘記引用控制器範圍,這只是你的發佈功能 – 2014-10-29 16:00:33

+1

好吧,現在更有意義,我會嘗試這聽起來不錯:) – Fabrizio 2014-10-29 16:03:33