gpt4 book ai didi

javascript - 从javascript访问 Angular Controller

转载 作者:行者123 更新时间:2023-11-28 03:23:37 25 4
gpt4 key购买 nike

我目前正在开发一个 angularJS 应用程序。我使用 Angular-Seed 作为启动项目的模板。

我需要创建一个在另一家公司的 JSP 页面上的 iframe 中运行的 angularJS 应用程序。他们使用 "iframe.contentWindow.postMessage" 在 iframe 中与我的应用程序通信。这是此类帖子的示例:

var btnHandShake = document.getElementById('btnHandShake');
btnHandShake.onclick = function () {
var dataObject = {
messagetype: "HANDSHAKE",
messagecontent: {
user: {
username: "username",
password: "password"
}
}
}

var message = JSON.stringify(dataObject);
iframe.contentWindow.postMessage(message, "https://serverurl:8080");
};

目前我的 angularJS 应用程序中有两个页面:

'use strict';


// Declare app level module which depends on filters, and services
angular.module('myApp', [
'ngRoute',
'myApp.filters',
'myApp.services',
'myApp.directives',
'myApp.controllers'
]).
config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/loading', {templateUrl: 'partials/loading.html', controller: 'LoadingController'});
$routeProvider.when('/view2', {templateUrl: 'partials/partial2.html', controller: 'MyCtrl2'});
$routeProvider.otherwise({redirectTo: '/loading'});
}]);

loading.html 页面如下所示:

<style>
.spinner {
position: fixed;
top: 50%;
left: 50%;
}
</style>

<script>
var opts = {
lines: 11, // The number of lines to draw
length: 15, // The length of each line
width: 10, // The line thickness
radius: 30, // The radius of the inner circle
corners: 1, // Corner roundness (0..1)
rotate: 0, // The rotation offset
direction: 1, // 1: clockwise, -1: counterclockwise
color: '#000', // #rgb or #rrggbb
speed: 0.6, // Rounds per second
trail: 60, // Afterglow percentage
shadow: false, // Whether to render a shadow
hwaccel: false, // Whether to use hardware acceleration
className: 'spinner', // The CSS class to assign to the spinner
zIndex: 2e9, // The z-index (defaults to 2000000000)
top: 'auto', // Top position relative to parent in px
left: 'auto' // Left position relative to parent in px
};

var spinner = null;
var spinner_div = 0;

$(document).ready(function () {
spinner_div = $('#spinner').get(0);
if (spinner == null) {
spinner = new Spinner(opts).spin(spinner_div);
} else {
spinner.spin(spinner_div);
}
});
</script>

<div id='spinner' class='spinner'></div>

Controller 看起来像这样:

'use strict';

/* Controllers */

angular.module('myApp.controllers', []).
controller('LoadingController', ['$scope', function ($scope) {

}])

.controller('MyCtrl2', [function () {

}]);

我需要一个额外的 javascript 文件来监听这个 iframe.contentWindow.postMessage 事件。它看起来像这样:

function postMessageListener(event) {
registerIncoming(event);

//convert data to object

var message = JSON.parse(event.data);
var responseType = "";

if (message.messagetype === "HANDSHAKE") {
responseType = "HANDSHAKE_OK";
}

if (responseType !== "") {
var response = { messagetype: responseType};
sendResponse(JSON.stringify(response), event.origin);
} else {
console.log("Unknown messagetype");
}
}

function sendResponse(response, origin) {
window.top.postMessage(response, origin);
}

//Listen to message from child window
if (window.addEventListener) {
// For standards-compliant web browsers
window.addEventListener("message", postMessageListener, false);
} else {
window.attachEvent("onmessage", postMessageListener);
}

function registerIncoming(event) {

var message = JSON.parse(event.data);

if (message.messagetype === "HANDSHAKE") {

var applicationScope = angular.element($("#ng-view-id")).scope();

}

$("#logger").prepend('<li class="bg-info">' + new Date().toLocaleTimeString() + " " + message + "</li>");
}

问题是我想从 'message.messagetype === "HANDSHAKE"' if check 中与 Controller 通信。但我似乎无法访问它。是因为我的 index.html 上有这个 ng-view 吗?也许还有另一种方法可以做到这一点。欢迎提出任何建议!

亲切的问候!

最佳答案

看起来我不需要 javascript 来与 iframe 通信,但 angularJS 本身是可能的。

https://github.com/newtriks/angularjs-post-message-testing-project

关于javascript - 从javascript访问 Angular Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22634583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com