Ng-click ( ngClick ) not working
No edit summary
No edit summary
Line 13: Line 13:
==== Doing it the proper way ====
==== Doing it the proper way ====
It is easy to declare a directive in angular and MDriven. Add this to your .js file of the Ext_Component:
It is easy to declare a directive in angular and MDriven. Add this to your .js file of the Ext_Component:
  function SomeFunctionToCallThatNeedContextData(data) {   
  function SomeFunctionToCallThatNeedContextData(ev,data) {   
     var x=data.SomeViewModenColumnName;
     var x=data.SomeViewModenColumnName;
     data.Execute("SomeViewModelAction");
     data.Execute("SomeViewModelAction");
Line 25: Line 25:
                 link: function (scope, element, attr)  
                 link: function (scope, element, attr)  
                 {
                 {
element[0].addEventListener("click",function(ev{SomeFunctionToCallThatNeedContextData(scope.data);});
                              element[0].addEventListener("click",function(ev)
                                    {SomeFunctionToCallThatNeedContextData(ev,scope.data);}
                                                        );
                 }             
                 }             
           };         
           };         
     }]);     
     }]);     
   console.trace("PortalConsumer Loaded");  
   console.trace("YOURNAME Loaded");  
  }  
  }  
   
   
  InstallTheDirectiveFor_PortalConsumer(angular.module(MDrivenAngularAppModule));
  InstallTheDirectiveFor_YOURNAME(angular.module(MDrivenAngularAppModule));

Revision as of 15:36, 23 May 2020

ng-click is the angular replacement for onclick event.

If you try to this in a simple component you get an error since you cannot bind into javascript - only html:

... onclick="CallFunction({{data.SomeValue}})" ....

If you try the below code nothing happens - due to your CallFunction is not part of the scope - it is outside the scope:

... ng-click="CallFunction(data.SomeValue)" ....

The kosher way is to define a directive ( a directive is a definition that enables you to tie html-elements to javascript-scope-data )

But if you want to solve with minimum effort I suggest you do this workaround:

...p1="{{data.SomeValue}}"  onclick="CallFunction(this.getAttribute('p1'))" ....

What the above code does: we use angular's ability have bindings in html - but we use it in an attribute we defined ourselves. Then we use javascripts ability to access that attributes' value and call our function.

Doing it the proper way

It is easy to declare a directive in angular and MDriven. Add this to your .js file of the Ext_Component:

function SomeFunctionToCallThatNeedContextData(ev,data) {  
   var x=data.SomeViewModenColumnName;
   data.Execute("SomeViewModelAction");
} 


function InstallTheDirectiveFor_YOURNAME(streamingAppController) {  
   streamingAppController.directive('yourdirectivename', ['$document', function ($document) 
  {            
    return {                
               link: function (scope, element, attr) 
               {
                              element[0].addEventListener("click",function(ev)
                                    {SomeFunctionToCallThatNeedContextData(ev,scope.data);}
                                                        );
               }            
          };        
   }]);    
  console.trace("YOURNAME Loaded"); 
} 

InstallTheDirectiveFor_YOURNAME(angular.module(MDrivenAngularAppModule));
This page was edited 98 days ago on 02/10/2024. What links here