如何在ng-change事件中调用预定义的JavaScript函数?

时间:2017-05-03 10:53:19

标签: angularjs

这是我的代码,但从未调用过ng-change。有谁知道为什么?感谢

<html ng-app="">

<head>
</head>

<body data-ng-init="names=['A', 'B', 'C']">

<script src="resources/js/angular/angular.min.js"></script>
<script src="resources/js/angular/angular.min.js.map"></script>

<select class="form-control input-sm"
        ng-model="fda"
        ng-change="alert('changed')"
        ng-options= "value for value in names" >
</select>


</body>

</html>

2 个答案:

答案 0 :(得分:2)

你需要一个控制器和一个相应的功能,

app.controller("myCtrl", function($scope) {
$scope.alert = function (){
    alert("changed");
}
});

<强>样本

var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
 $scope.alert = function (){
    alert("changed");
}
});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<body ng-app="myApp" ng-controller="myCtrl">
    <div ng-init="names=['A', 'B', 'C']">
        <select class="form-control input-sm" ng-model="fda" ng-change="alert('changed')" ng-options="value for value in names">
     </select>
    </div>
</body>

</html>

答案 1 :(得分:0)

只需在控制器var app = angular.module("myApp", []); app.controller("myCtrl", function($scope) { $scope.alert = alert.bind(window); });

中添加此行即可

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<body ng-app="myApp" ng-controller="myCtrl">
    <div ng-init="names=['A', 'B', 'C']">
        <select class="form-control input-sm" ng-model="fda" ng-change="alert('changed')" ng-options="value for value in names">
     </select>
    </div>
</body>

</html>
List<Integer> list = new ArrayList<>();
Scanner myScanner = new Scanner(System.in);
myScanner.useDelimiter(System.getProperty("line.separator"));
System.out.println("give some int...");
while (myScanner.hasNextInt()) {
       list.add(myScanner.nextInt());
}

myScanner.close();
System.out.println("now calculating");
doMath(list);
System.out.println("done");