秘银与angularjs [英] Mithril with angularjs

查看:175
本文介绍了秘银与angularjs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新手,秘银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>

这篇关于秘银与angularjs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆