2016-06-28 21 views
0

我是AngularJS的新手。任何人都可以告訴我如何在另一個控制器的作用域函數中調用自定義指令。如何在另一個控制器的示波器功能中調用自定義指令

比如我有一個功能的控制器如下:

angularApp.controller('sample_Ctrl', function ($scope, $http, $timeout, $rootScope, $location) { 
    $scope.showReport = function(id) { 
    }; 
}); 

我創建了一個customDirective如下:

var showModalDirective = function ($timeout) { 
    return { 
     restrict: 'E', 
     templateUrl: '/Partials/template1.html', 
    }; 
}; 
angularApp.directive('showModal', showModalDirective); 

那麼如何調用此指令showReport功能,以及如何我可以將id傳遞給模板URL嗎?

+0

你的問題不明確。你能添加一些上下文/例子嗎? –

+0

**你打電話給**是什麼意思? – Mehraban

+0

我添加了一個例子,你們可以現在檢查 – user1268130

回答

0

您不能在controller中致電directive。它應該是一個service

或者你可以考慮使用指令:

指令

var showModalDirective = function ($timeout) { 
    return { 
     restrict: 'E', 
     scope: { 
      modalId: '=' 
     }, 
     templateUrl: '/Partials/template1.html', 
    }; 
}; 

angularApp.directive('showModal', showModalDirective); 

控制器:

angularApp.controller('sample_Ctrl', function ($scope, $http, $timeout, $rootScope, $location) { 

    $scope.showModal = false; 

    $scope.showReport = function(id) { 
    $scope.showModal = true; 
    $scope.modalId = id; 
    }; 
}); 

查看

<div ng-if="showModal"> 
    <show-modal modal-id="modalId"></show-modal> 
</div> 

showModal指令是在showModal變量爲true時調用。

0

要首先使用您的指令,您需要創建與您的指令具有相同名稱的HTML元素,然後從您的控制器爲該指令提供數據。假設你的html頁面有一個div。

<div show-modal> </div> 

接着這個頁面上您template1.html將在內部通過調用指令,並假設有在template1.html一些html元素像

code in your controller - 
angularApp.controller('sample_Ctrl',function() { 
    $scope.firstName= "My First Name" 
}) 
相關問題