如何在AngularJS控制器中调用jQuery函数?

时间:2017-12-07 11:40:37

标签: jquery angularjs

我有以下html:

<iframe name='iframe1' id="iframe1" src='http://micro.shoretel.com" frameborder='0' width='660' height='450'></iframe>

和jQuery:

$("iframe").contents().find("element-selector").css("border-color", "blue");   


我想这样做:

app.controller("myCntrl", function ($scope, angularService, $modal) {

        $scope.deletePrepared = function (itm) {
            var getData = angularService.DeletePrepared(itm.ProductId);
            getData.then(function (msg) {
                GetAllPrepared();
            }, function () {
$("iframe").contents().find("element-selector").css("border-color", "blue");});}});

可能是什么错误?

3 个答案:

答案 0 :(得分:0)

var app = angular.module("Profile",[])
app.controller("ProfileCtrl", function($scope){
  window.call_from_scope = function(){
        alert('CALLED FROM SCOPE')
      }
  $scope.get_name = function(){
        window.call_from_scope()
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="Profile" ng-controller="ProfileCtrl">
    <button ng-click="get_name()">Click Here</button>
</body>

答案 1 :(得分:0)

//THIS IS BOTH WAY COMMUNICATION U CAN CALL FROM JS TO CONTROLLER AND CONTROLLER TO JS
window.call_from_scope = function(){
        alert('CALLED FROM SCOPE')
      var myscope= angular.element(document.querySelector('[ng-app="Profile"]')).scope()
      myscope.return()
  }
var app = angular.module("Profile",[])
app.controller("ProfileCtrl", function($scope){
 
  $scope.get_name = function(){
        window.call_from_scope()
  }
   $scope.return = function(){
    alert('Called from window')
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="Profile" ng-controller="ProfileCtrl">
    <button ng-click="get_name()">Click Here</button>
</body>

答案 2 :(得分:0)

来自文档:

  

要使用jQuery,只需确保在angular.js文件之前加载它。您还可以使用ngJq指令指定jqlite应该使用jQuery,或者如果页面上存在多个版本,则使用特定版本的jQuery。

     

—AngularJS angular.element API Reference