秘银与angularjsangularjs

2023-09-13 02:41:17 作者:薄荷蓝

我是一个新手,秘银JS框架,并试图整合与angularJS Mitril视图。有没有人尝试过​​呢?

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

我要检查我们如何能够结合的角度控制方法,单击Mitril创建的元素的事件。

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

我有了这个code得到了这个工作。

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.

推荐答案

我说,是不是地道的角度code。

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() {
            $scope.$eval(attrs.onclick)
          })
        }
      })
      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>
</div>