43

文本框中的数据时如何调用角度函数?这是我的代码

html

<!DOCTYPE html!>
<html xmlns:ng="http://angularjs.org" data-ng-app="flightSearch" lang="en">
    <head>
        <script src="js/jquery-1.9.1.js"></script>
        <script src="js/jquery-ui.js"></script>
        <script src="js/app.js"></script>
    </head>
    <body> 
        <div data-ng-controller="flightSearchController">

<input type="text" data-ng-model="query" data-ng-change="flightSearch()" placeholder="e.g RGN>MDL, KAW>RGN"  id="targetTextField"  />

<input id="options3" type="text" style="display:none;"/>



<select id="options">
  <option value="From" selected>From</option>
  <option value="RGN" ng-click="flightSearch()">RGN</option>
  <option value="NYU" ng-click="flightSearch()">NYU</option>
  <option value="MDL" ng-click="flightSearch()">MDl</option>
  <option value="HEH" ng-click="flightSearch()">HEH</option>
</select>

<select id="options2">
  <option value="To" selected>To</option>
  <option value=">RGN" ng-click="flightSearch()">RGN</option>
  <option value=">NYU" ng-click="flightSearch()">NYU</option>
  <option value=">MDL" ng-click="flightSearch()">MDl</option>
  <option value=">HEH" ng-click="flightSearch()">HEH</option>
</select>

            <ul>
                <li data-ng-repeat="route in filteredItems"> 
                    <div data-ng-bind-html="route.displayRoute"></div>
                </li>
            </ul>

        </div>
    </body>

js

<script src="js/angular.min.js"></script>
    <script src="js/angular-sanitize.min.js"></script>
    <script type="text/javascript">
        function buildPaths(route){
            var points = route.split(">");
            var paths = {};
            for(var i = 0; i < points.length; i++){
                var from = points[i];
                for(var j = i; j < points.length; j++){
                    var to = points[j];
                    if (from != to){
                        if(paths[from + ">" + to]){
                            if(j-i < paths[from + ">" + to][0])
                                paths[from + ">" + to] = [j-i,points.slice(i,j+1).join(">")]
                        }else{
                            paths[from + ">" + to] = [j-i,points.slice(i,j+1).join(">")]
                        }
                    }
                }//for j
            }//for i
            return paths;
        }

        var flightSearch = angular.module('flightSearch',['ngSanitize']);
        flightSearch.controller('flightSearchController', function ($scope){

            // Data definitions, route is actual data,
            // display route is for higlight display purpose only.
            var dataRoutes = [
            {route: "RGN>NYU>MDL>HEH>RGN", displayRoute: "", paths: []},
            {route: "RGN>HEH>KYT>THK>KYT>HEH>RGN", displayRoute: "", paths: []},
            {route: "RGN>MDL>HEH>NYU>RGN", displayRoute: "", paths: []},
            {route: "RGN>MDL>STW>RGN", displayRoute: "", paths: []},
            {route: "RGN>NYU>MDL>MYT>PTO>MYT>MDL>NYU>RGN", displayRoute: "", paths: []},
            {route: "RGN>HEH>NYU>MDL>RGN", displayRoute: "", paths: []},
            {route: "RGN>TVY>MGU>KAW>MGU>TVY>RGN", displayRoute: "", paths: []},
            {route: "RGN>TVY>KAW>TVY>RGN", displayRoute: "", paths: []},
            {route: "RGN>KAW>MGU>TVY>RGN", displayRoute: "", paths: []}];

            // Pre-calculate the possible path the shortest distance
            for(var r = 0; r< dataRoutes.length; r++){
                dataRoutes[r]["paths"] = buildPaths(dataRoutes[r]["route"]);
                dataRoutes[r]["displayRoute"] = dataRoutes[r]["route"];
            }

            // Assign the routes. This is unfiltered origintal data (all)
            $scope.routes = dataRoutes;

            // This is filtered list for dispaly
            $scope.filteredItems = $scope.routes;

            // Search/filter function. This updates filteredItems list
            $scope.flightSearch = function(){


                if($scope.query.length > 6){
                    $scope.filteredItems = [];
                    for(var index in $scope.routes){
                        $scope.routes[index]["displayRoute"] = $scope.routes[index]["route"];
                        if($scope.routes[index]["paths"][$scope.query] !== undefined){
                            var match = $scope.routes[index]["paths"][$scope.query][1];$scope.query
                            $scope.routes[index]["displayRoute"] = $scope.routes[index]["route"].replace(match,"<b>" + match + "</b>");
                            $scope.filteredItems.push($scope.routes[index]);
                        }
                    }
                }else{
                    for(var r = 0; r< $scope.routes.length; r++){
                        $scope.routes[r]["displayRoute"] = $scope.routes[r]["route"];
                    }
                    $scope.filteredItems = $scope.routes;
                }
            }
        });
        flightSearch.filter("highlight",function(){
                return function(text){
                        return text;
                }
        });
    </script>
    <script type="text/javascript">
        $(function() {
$("#options").change(function(){
        setTarget()
});
    $("#options2").change(function(){
        setTarget();
});
    $("#options3").change(function(){
        setTarget();
});
    $("#targetTextField").change(function(){
        setTarget();
});
});

function setTarget(){
    var tmp = $("#options").val();
    tmp += $("#options2").val();
    tmp += $("#options3").val();
    $('#targetTextField').val(tmp);
}


    </script>
</html>

当两个选择选项在文本框中插入数据想要在 change.strong 文本上调用搜索功能时

4

7 回答 7

99

如果您将 ID 标签附加到与 ng-controller 相同的 DOM 元素,则可以访问 angular 元素的范围:

DOM:

<div id="mycontroller" ng-controller="mycontroller"></div>

你的控制器:

function mycontroller($scope) {
   $scope.myfunction = function() {
      //do some stuff here
   }
}

在 jquery 你这样做,它会访问该控制器并调用该函数:

angular.element('#mycontroller').scope().myfunction();

记得打电话

angular.element('#mycontroller').scope().$apply() 

如果您在 范围内更新函数变量myfunction,否则它将无法正常工作(感谢@andersh)

于 2013-10-15T08:16:48.633 回答
16

如果你在你的代码中使用这样的东西

<div id="mycontroller" ng-controller="mycontroller as child"></div>

代替

<div id="mycontroller" ng-controller="mycontroller"></div>

访问将是

angular.element('#mycontroller').scope().child.myFunction();

代替

angular.element('#mycontroller').scope().myFunction();

在任何情况下,您还需要调用

angular.element('#mycontroller').scope().$apply();
于 2015-09-16T09:43:51.823 回答
7

用于访问控制器的范围元素

angular.element(document.getElementById('controllerId')).scope().scope_variable_array.push(item);

用于访问控制器功能

angular.element(document.getElementById('controllerId')).scope().function_name();

controllerId : ng-controller 在 HTML 中定义的标签 id

于 2014-01-06T09:13:13.970 回答
4

我参考这篇文章,将函数分配给全局对象window

            // In angularJS script
            $scope.foo = function() {
                console.log('test');
            };
            $window.angFoo = function() {
                $scope.foo();
                $scope.$apply(); 
            };

            // In jQuery
            if (window.angFoo) {
                window.angFoo();
            }
于 2017-10-26T05:47:58.980 回答
4

我使用这样的东西:

var $scope = angular.element($('[ng-controller]')).scope()

并操纵$scope变量。您可以在控制台中查看

于 2017-09-18T16:17:42.993 回答
1

我使用此代码并且运行良好。

 var $scope = angular.element($('[ng-controller="MapCtrl"]')).scope();

并感谢@Ivan-San 的回答。

于 2017-12-17T16:01:49.580 回答
0

您可以将 Jquery 代码放入 AngularJS 控制器中,例如

    $scope.shipchange = function (SelectedShipId ) {
        alert('ShipId =' + SelectedShipId );
    }

    $("#Ships").change(function () {

        SelectedShipId = $("#Ships").val();
        $scope.shipchange(SelectedShipId );
    })
于 2018-11-22T07:26:39.477 回答