Saravanan S Saravanan S - 1 year ago 65
AngularJS Question

Mithril with angularjs

I am a newbie to Mithril JS framework and trying to integrate Mitril view with angularJS. Has anyone tried this before?

I want to check how can we bind the angular controller methods to click events of elements created in Mitril.

I got this working by having this code

var e = document.getElementById('elementId');
var scope = angular.element(e).scope();
m("a[href='javascript:;']", {
onclick : scope.someMethod
}, "Test");

But I am not sure if this is right way to do this.

Answer Source

I'd say that is not idiomatic angular code.

A more idiomatic way might be to use a directive on the Angular side, and pass in an event dispatcher controller to the view on the mithril side:

//mithril code
var testWidget = function(ctrl) {
  return m("a[href='javascript:;']", {onclick: ctrl.onclick}, "Test")

//angular code
angular.module("foo").directive("testWidget", function() {
  return {
    restrict: "E",
    link: function($scope, element, attrs) {
      var template = testWidget({
        onclick: function() {
          $scope.$apply(function() {
      m.render(element, template)

angular.module("foo").controller("MyCtrl", function() {
  this.doStuff = function() {
    console.log("called doStuff")

<div ng-controller="MyCtrl as c">
  <test-widget onclick="c.doStuff()"></test-widget>