我试图在视频结束后执行一个函数,该函数位于 Angular Controller 内。该函数被调用,但我收到一条错误消息:
ReferenceError: postVendor is not defined
这是调用的 javascript:
$.noConflict();
jQuery(document).ready(function ($) {
var api = flowplayer();
api.bind("finish", function (e, api) {
var data = "true";
var url = "someurlhere";
postVendor(url, data);
});
});
这是我的模块/ Controller (同一文件):
var registrationModule = angular.module('registrationModule', []);
registrationModule.controller("vendorCtrl", function ($scope, $http) {
$scope.postVendor = function (url, data) {
$http.post(url, data).success(function (data) { console.log(data) }).error(function (data) { console.log(data) });
};
});
在我的html中:
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="registrationModule">
<body ng-controller="vendorCtrl">
我使用的是 Angular 1.2.25。
关于如何正确调用函数有什么想法吗?
最佳答案
1.请确保 jQuery 在 angular.js 之前加载
2.确保你的vendorCtrl返回$scope.postVendor(你可以通过添加来做到这一点):
return {
postVendor : $scope.postVendor
};
请参阅示例
$(document).ready(function () {
angular.element('#angularController').controller().doSomthing("Hello ", "from jquery")
});
var app = angular.module('app', []);
app.controller('firstCtrl', function($scope){
$scope.doSomthing = function(a,b){
alert(a+b);
};
return {
doSomthing: $scope.doSomthing
};
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="app" >
<div id="angularController" ng-controller="firstCtrl">
</div>
</body>
$.noConflict();
jQuery(document).ready(function ($) {
var api = flowplayer();
api.bind("finish", function (e, api) {
var data = "true";
var url = "someurlhere";
angular.element('body').controller().postVendor(url, data);
});
});
有关更多信息,请参阅此处 Call Angular JS from legacy code
关于javascript - 函数未在 Controller 内部定义,AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26021933/