简体   繁体   English

Extjs面板内的角度

[英]Angular inside of an Extjs Panel

Disclaimer: it's not my choice to put angular inside of an Extjs panel. 免责声明:在Extjs面板中放入角形不是我的选择。 I have certain business needs that require this, so spare me the, "you shouldn't do this" answers. 我有某些需要这样做的业务需求,因此请避免回答“您不应该这样做”的答案。

I'm writing a mobile webapp with extjs 6.0.1. 我正在用extjs 6.0.1编写移动Web应用程序。 For various reasons out of my control, I need to be able to use angular in my extjs app. 由于无法控制的各种原因,我需要能够在extjs应用程序中使用angular。 My first attempt at this is to just display an Ext Panel and put angular markup in the html config, like this: 我的第一个尝试是仅显示一个Ext Panel,并将角度标记放入html配置中,如下所示:

  this._angularPanel = Ext.create('Ext.Panel', {
    height: '100%',
    width: '100%',
    cls: 'angularPanel',
    html:
        '<div ng-app="">'+
          '<p class="angTest">Name : <input type="text" ng-model="name"></p>'+
          '<h1>Hello {{name}}</h1>'+
        '</div>'
  });

I also have included the script at the top of the page: 我还在页面顶部添加了脚本:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script>

However, it is just parsing it like plain old HTML in my panel, so the result is this: 但是,它只是像在面板中解析旧的HTML一样解析它,所以结果是这样的:

在此处输入图片说明

I have also tried using a template like this: 我也尝试过使用这样的模板:

  this._angularPanel = Ext.create('Ext.Panel', {
    height: '100%',
    width: '100%',
    cls: 'angularPanel',
    tpl: [
        '<div ng-app="">'+
          '<p class="angTest">Name : <input type="text" ng-model="name"></p>'+
          '<h1>Hello {{name}}</h1>'+
        '</div>'
        ],
    data: {}
  });

In this case the template thinks the name is an argument for the Ext template, which make sense since it's in the brackets, so the screen looks like this instead: 在这种情况下,模板认为名称是Ext模板的自变量,这是有意义的,因为它在方括号中,因此屏幕看起来像这样:

在此处输入图片说明

Any ideas? 有任何想法吗?

EDIT: 编辑:

Now also have tried bootstrapping it manually: 现在也尝试手动引导它:

  this._angularPanel = Ext.create('Ext.Panel', {
    height: '100%',
    width: '100%',
    cls: 'angularPanel',
    html:
      '<div ng-controller="MyController"> '+
         'Hello {{greetMe}}! ' +
      '</div> '+
      '<script src="http://code.angularjs.org/snapshot/angular.js"></script> '+
          '<script> '+
          'angular.module("myApp", []).controller("MyController", ["$scope", function ($scope) { '+
                '$scope.greetMe = "World"; '+
              '}]); '+

          'angular.element(function() { '+
            'angular.bootstrap(document, ["myApp"]); '+
          '}); '+
      '</script>'
  });

Still doesn't work. 仍然不起作用。 I get: 我得到:

Hello {{greetMe}} 您好{{greetMe}}

instead of: 代替:

Hello World 你好,世界

Ok I got angular working in an Extjs Panel's html config. 好的,我在Extjs Panel的html配置中进行了工作。 Turns out extjs doesn't like script elements in the html config, so we need to place them elsewhere. 事实证明extjs不喜欢html配置中的脚本元素,因此我们需要将它们放置在其他位置。 Here's what I did. 这就是我所做的。

At the top of my document in the where I include all my extjs files and angular, I added this: 在我的文档顶部,其中包括所有extjs文件和angular文件,我添加了以下内容:

  function bootstrapAngular() {
    angular.module('myApp', [])
        .controller('MyController', ['$scope', function ($scope) {
          $scope.greetMe = 'World';
        }]);

    angular.element(function() {
      angular.bootstrap(document, ['myApp']);
    });
  }

Then, within Extjs (in my case I have a card panel and I swap over to an empty panel with angular html inside of it and then call the bootstrap method: 然后,在Extjs中(在我的情况下,我有一个卡片面板,我将其切换到其中带有angular html的空面板,然后调用bootstrap方法:

Here's the panel containing angularjs markup: 这是包含angularjs标记的面板:

  this._angularPanel = Ext.create('Ext.Panel', {
    height: '100%',
    width: '100%',
    cls: 'angularPanel',
    html:
      '<div ng-controller="MyController"> '+
         'Hello {{greetMe}}! ' +
      '</div> '
  });

Then when I want to set this panel active I just do: 然后,当我想将此面板设置为活动状态时,我只是这样做:

    this.setActiveItem(this._angularPanel);
    bootstrapAngular();

Using this method, you can easily inject Angularjs markup into extjs panels. 使用这种方法,您可以轻松地将Angularjs标记注入extjs面板。 This is a simple example but it's plain to see that this method should allow for any amount of complexity needed. 这是一个简单的示例,但很明显,此方法应允许所需的任何复杂程度。

Special thanks to estus for pointing me in the bootstrapping direction. 特别感谢estus指出了我的引导方向。

I was also stuck on this issue. 我也被这个问题困扰。 I found the solution from Slims answer. 我从Slims答案中找到了解决方案。 I got the idea from Slims answer and I applied in the simple code. 我从Slims答案中得到了这个想法,并在简单的代码中得到了应用。 Here is a fiddle example for simple integration of AngularJS into Extjs panel :- 这是一个将AngularJS简单集成到Extjs面板中的小提琴示例:-

this._angularPanel = Ext.onReady(function() {
Ext.create('Ext.Panel', {
    renderTo: 'helloWorldPanel',
    height: 100,
    width: 200,
    title: 'Hello world',
    html: '<div ng-app="myApp" ng-controller="MyController"> '+
    'Hello {{greetMe}}! ' +
    '<br><br>' +
    '<button ng-click="changeGreeting()">Change Greeting</button>' +
    '</div> '
});
bootstrapAngular();

}); });

click here for fiddle 点击这里小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM