如何在ngClick上获取参数。然后,触发该指令,以注入dinamically模板

问题描述:

我需要根据单选按钮的值或参数动态注入新模板。
这是我的HTML如何在ngClick上获取参数。然后,触发该指令,以注入dinamically模板

<div class="container-fluid" ng-app="rjApp"> 
     <div class="panel-body" ng-controller="mainController"> 
      <input name="penanggung_biaya" type="radio" ng-model="checked" ng-click="broadcast(umum)" class="ace" value="umum"> <!-- I though, umum is the parameter which I want passed through to the Controller -->  
      <input name="penanggung_biaya" type="radio" ng-model="checked" ng-click="broadcast(instansi)" class="ace" value="instansi"> 

<example-directive message="message"> </example-directive> 

     </div> 
</div> 

和,这是JS

var rjApp = angular.module('rjApp',[]); 
    rjApp.config(function($interpolateProvider) { 
     $interpolateProvider.startSymbol('{::'); 
     $interpolateProvider.endSymbol('::}'); 
    }) 


//mainCotroller, should be work by ngClick through radio button 
    function mainController($scope, $rootScope) { 
     $scope.broadcast = function(event){ 
     console.log(event) //I've been thought, this is the part to passing the parameter of radio button, but not gonna works. 
      $rootScope.$broadcast('handleBroadcast'); 
     }; 
    } 


//the Directive, should be injected dinamically template, depends on ngClick parameter inside radion button 
    rjApp.directive('exampleDirective', function() { 
     return { 

      restrict: 'E', 
      scope: { 
       message: '=' 
      }, 
      link: function(scope, elm, attrs) { 
       scope.$on('handleBroadcast', function(doifq) { 
        templateUrl= '<?php echo url("registrasi/rawat_jalan/penanggung_biaya/") ?>'+doifq //This is the part to injected the dinamically template. And I've been guess the **doifq**, is the paramter to passing by the mainController 
       });      
      }, 
     }; 
    }); 

请,有人帮助我。
问候。

+0

你想只是切换模板或重新启动你的指令吗? –

+0

@DayanMorenoLeon只是切换模板。如果我选择收音机,然后模板正在切换。 –

In broadcast, you could pass the parameter, 
    $scope.broadcast = function(event){ 
     console.log(event); 
     $rootScope.$broadcast('handleBroadcast',event); 
    }; 
This way you would be getting doifq value in directive depending on the which radio button is clicked. 
+0

如果我选择了单选按钮,我想先获取参数。我想看到console.log返回='umum'或'instansi'。但是现在,console.log(event)//返回undefined –

+0

在ng中单击字符串格式的传递值。这里umum是未定义的变量,因此在控制台中它返回undefined。尝试ng-click =“广播('umum')”和ng-click =“广播('instansi')”“。 –

+0

是的,这是在mainController工作,显示umum和instansi。但是,如何在指令中获取该参数?如何将该参数传递给指令?谢谢。 –