AngularJS问题:路由后第二页未调用指令
AngularJS issue: Directive not called on Second Page after Routing
我正在开发一款angular应用程序
我正在绑定角度指令mytoggle
来切换页面中的内容,但如果我通过角度路由转到第二个页面,则切换仅在第一个页面上起作用,不会调用相同的指令,因此切换也不起作用。
我创建了一个要点,要点链接网址:https://gist.github.com/shmdhussain/c802c7a59c78d8e498da
Index.html:
<html xmlns:ng="http://angularjs.org" lang="en" id="ng-app" ng-app="myApp">
<head>
<meta charset="utf-8">
<title>Angular App</title>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.15/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.15/angular-route.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="app.js"></script>
<script src="js/controllers/ctrl.js"></script>
<style type="text/css">
.show{display:none}
</style>
</head>
<body>
<div ng-controller="parentCtrl" class="">
<div ng-view>
</div>
</div>
</body>
</html>
默认.html
<p><a href="#/p1">page 1</a></p>
<p><a href="#/p2">page 2</a></p>
Page1.html:
<div>
<p mytoggle class="toggle">Toggle ME Page 1</p>
</div>
<div class="show">
<p>In Page One</p>
<p>In Page One</p>
<p>In Page One</p>
<p>In Page One</p>
<p>In Page One</p>
<p>In Page One</p>
<p>In Page One</p>
<p>In Page One</p>
</div>
Page2.html:
<div>
<p mytoggle class="toggle">Toggle ME Page2</p>
</div>
<div class="show">
<p>In Page Two</p>
<p>In Page Two</p>
<p>In Page Two</p>
<p>In Page Two</p>
<p>In Page Two</p>
<p>In Page Two</p>
</div>
app.js:
// Create a new module
var myApp = angular.module('myApp', ['ngRoute']);
// register a new service
//myApp.value('appName', 'MyCoolApp');
// configure existing services inside initialization blocks.
myApp.config(function($locationProvider,$routeProvider) {
$routeProvider
.when('/p1', {
templateUrl:'partials/page1.html'
})
.when('/p2', {
templateUrl:'partials/page2.html'
})
.when('/default', {
templateUrl:'partials/default.html'
.otherwise({
redirectTo:'/default'
});
});
ctrl.js:
myApp.controller('parentCtrl',['$scope','$window','$location',function ($scope,$window,$location) {
}]);
myApp.directive('mytoggle',function(){
console.log("inside dir");
//ALERTS
jQuery(".toggle").click(function (e) {
console.log("ddd");
console.log("class names: "+jQuery(e.target).attr('class'));
jQuery(".show").slideToggle("slow");
});
return true;
});
myApp.directive('mytoggle', function() {
// The injecting function of the directive.
// Executed only once for the entire app (if the directive is used).
console.log('Injecting function says hello.');
// Simplest form of returning postLink function
return function postLink() {
// The postLink function of the directive.
// Executed once per instance of the directive, each time it's rendered.
console.log('postLink says hello.');
jQuery(".toggle").click(function(e) {
jQuery(".show").slideToggle("slow");
});
};
});
如果你想把它作为一个指令,我至少建议如下:
myApp.directive('mytoggle', function() {
return {
restrict: 'A',
link: function postLink(scope, element, attrs) {
var onClick = function() {
$('.show').slideToggle("slow");
};
element.on('click', onClick);
scope.$on('$destroy', function() {
element.off('click', onClick);
});
}
};
});
相关文章:
- 在指令控制器中使用$attrs时出现问题
- 在<页眉>标签
- 参数变量出现ngTable指令问题
- 使用javascript将动态表从一个html页面打印到另一个html页
- 如何使用skip参数使用angular ui引导进行服务器端分页
- 正在SharePoint 2013母版页中添加JQuery移动文件
- 在AngularJS应用程序中使用封装指令和路由的推荐方式是什么
- 当uib分页被包装在另一个指令中时,AngularJS表数据没有更新
- 为一个指令编写测试,该指令在因果报应中进行整页重新加载
- 角js中的百慕大三角:$resource+指令+分页
- 如何从分页指令angularjs隐藏直接页面链接
- 如何使用AngularJS中的自定义指令将一个html页面插入另一个html页
- 更改页面的Redraw指令
- 如何在通过ajax调用的页面上有angularJS指令
- 带有AngularUI引导分页指令的AngularJS不会隐藏结果
- AngularJS:指令多次出现在页面上,并发出/广播处理
- AngularJS问题:路由后第二页未调用指令
- 如何运行“加二”;图像幻灯片放映”;在一页上
- 分页器指令的动态项模板
- Angularjs分页指令