【问题标题】:Function is not defined inside controller, angularJS函数未在控制器内部定义,angularJS
【发布时间】:2014-09-24 16:25:00
【问题描述】:

我正在尝试在视频结束后执行一个功能,该功能位于角度控制器内。该函数被调用,但我收到一条错误消息:

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);
            });
        });

这是我的模块/控制器(同一个文件):

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">

我正在使用角度 1.2.25。

关于如何正确调用函数的任何想法?

【问题讨论】:

    标签: javascript asp.net angularjs function controller


    【解决方案1】:

    1.请确保在angular.js之前加载jQuery

    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

    【讨论】:

    • 谢谢,我只是在尝试这种变化,但得到:Error: [jqLite:nosel] Looking up elements via selectors is not supported by jqLite!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-03
    • 2015-08-26
    • 1970-01-01
    • 2014-05-24
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多