我有一個簡單的帶有角度js的菜單代碼。當文件加載時,我發送一個數組來準備菜單。在這個順序中,我還將點擊事件寫入了生成的菜單。但是如果菜單準備之前的點擊處理程序不會觸發。Jquery事件與Angular js無法正常工作
當我改變點擊事件的順序後,菜單準備工作完美。
我在下面的代碼的評論中提到的工作順序。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<link rel="stylesheet" type="text/css" href="MenuStyle.css">
<title>Welcome</title>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
<div id='cssmenu' >
<ul>
<li ng-repeat="menus in menuList track by $index" ng-init="Index = $index" class='active has-sub open clickthis'>
<a><span>{{menus.mainmenu}}</span></a>
<ul>
<li ng-repeat="sub in menus.submenu" class='last'><a>{{sub}}</a></li>
</ul>
</li>
</ul>
</div>
</bodY>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$(document).ready(function(){
//Why this not handle the click event
//If i create the li by dynamically using jquery this will handle the click event.
//But angulr generated li does not handles.
//What is the reason.
$(".clickthis").click(function(){alert('hai');});
$scope.menuList= [
{mainmenu:'main1',submenu:['sub1','sub2']},
{mainmenu:'main2',submenu:['sub1','sub2','sub3']},
{mainmenu:'main3',submenu:['sub1','sub2','sub3','sub4']},
{mainmenu:'main4',submenu:['sub1','sub2']}
];
$scope.$apply();
$('#cssmenu li>ul').slideDown();
//Why the below line handle the click event
//What is the difference between above line and below line.
//$(".clickthis").click(function(){alert('hai');});
});
});
</script>
</html>
這是什麼原因或邏輯。請說清楚這一點。
可能重複http://stackoverflow.com/問題/ 22714286/call-jquery-function-from-angularjs-controller) –