[英]AngularJS trigger ng-change not working
我只想在單擊按鈕時簡單地觸發更改。 更改只會提醒文本。 但是觸發器不起作用,請幫忙。
這是代碼:
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<link rel="stylesheet" type="text/css" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
</head>
<body ng-controller="myCtrl">
<input type="text" ng-model="first.name"> <br />
<input type="checkbox" ng-model="same" id="sames" ng-change="sameAsAbove(first, second)"> Same as above <br />
<select ng-model="change" id="changes" ng-change="changeOver()">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<input type="text" ng-model="second.name">
<input type="text" ng-model="second.school">
<span ng-click="clickMe()">Click Me!</span>
<!-- <span id="clickMe">Click Me!</span> -->
{{ same }}
<script type="text/javascript">
app = angular.module('myApp', []);
app.controller('myCtrl', function($scope){
$scope.sameAsAbove = function(primary, receiver){
receiver['name'] = primary['name'];
};
$scope.clickMe = function(){
angular.element(document.getElementById('changes')).triggerHandler('change');
// x = angular.element(document.getElementById('changes')).val();
// alert(x);
}
$scope.changeOver = function(){
alert("deam");
}
});
$("#clickMe").click(function(){
// alert("czxvzx");
// $("#same").trigger("click");
$("#changes").change();
});
</script>
</body>
</html>
這是 plunker 的鏈接: http ://plnkr.co/edit/zErS4DaTgR79SBLbtGOy?p=preview
嘗試這個
<script type="text/javascript">
app = angular.module('myApp', []);
app.controller('myCtrl', function($scope,$timeout){
$scope.sameAsAbove = function(primary, receiver){
receiver['name'] = primary['name'];
};
$scope.clickMe = function(){
$timeout(function() {
angular.element(document.getElementById('changes')).triggerHandler('change');
}, 100);
// x = angular.element(document.getElementById('changes')).val();
// alert(x);
}
$scope.changeOver = function(){
alert("deam");
}
});
$("#clickMe").click(function(){
// alert("czxvzx");
// $("#same").trigger("click");
$("#changes").change();
});
</script>
通過添加 $timeout 您的問題得到解決。
你可以這樣做:-
$scope.clickMe = function(){
var scope = angular.element("#changes").scope();
scope.changeOver();
//angular.element(document.getElementById('changes')).triggerHandler('change');
}
工作副本在這里
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.