我在搜索和尝试后想出了这个解决方案,在你的情况下这个解决方案比
使用angular.element(....).scope() 的其他解决方案,因为这里我们使用角度服务$window 而在 (angular.element().scope) 我们使用的某些东西可能在某些角度模式下被禁用,check this。
CBR_XML_Daily_Ru = function(data) {
window.angularJsonpCallBackDefindInController(data);
};
var myApp = angular.module('myApp', []).controller('myCtrl', myCtrl);
function myCtrl($scope, $http,$window) {
var self = this;
$http.jsonp('https://www.cbr-xml-daily.ru/daily_jsonp.js?callback=JSON_CALLBACK');
$window.angularJsonpCallBackDefindInController = function (data) {
//u can do anything here, you are in the controller, and in the same time this function in the window object and get called from anywhere even in jquery
self.divForCharCode = data.Valute.EUR.CharCode;
};
}
您也可以使用这种方式从 jquery "scope" 中执行控制器中的功能:
CBR_XML_Daily_Ru = function(data) {
angular.element("#controllerDivId").controller().angularJsonpCallBackDefindInController(data);
};
var myApp = angular.module('myApp', []).controller('myCtrl', myCtrl);
function myCtrl($scope, $http,$window) {
var self = this;
$http.jsonp('https://www.cbr-xml-daily.ru/daily_jsonp.js?callback=JSON_CALLBACK');
self.angularJsonpCallBackDefindInController = function (data) {
self.divForCharCode = data.Valute.EUR.CharCode;
};
}
这是html(您需要包含jquery才能使用第二种解决方案)
<html>
<head>
<script src="jquery-3.1.1.min.js"></script>
<script src="angular.min.js"></script>
<!--<link rel="stylesheet" href="style.css" />-->
<script src="jsnopstuff.js"></script>
</head>
<body>
<div id="controllerDivId" ng-controller='myCtrl as ctrl' ng-app='myApp'>
<input type=text ng-model='ctrl.divForCharCode'>
<div id='div1'></div>
</div>
</body>
</html>