admin管理员组文章数量:1320837
I've just been assigned a website that was made entirely with jQuery. It load asynchronously a few pages, and want to be a SPA.
Now the only thing is that the developer didn't think about URL in general, and people can't access the site any other way than by www.example
I know AngularJS a bit and my question would be: -Is it worth integrating AngularJS to just manage the routing, just so I don't have to go through jQuery and checking each and every click, then each links?
Thanks
I've just been assigned a website that was made entirely with jQuery. It load asynchronously a few pages, and want to be a SPA.
Now the only thing is that the developer didn't think about URL in general, and people can't access the site any other way than by www.example.
I know AngularJS a bit and my question would be: -Is it worth integrating AngularJS to just manage the routing, just so I don't have to go through jQuery and checking each and every click, then each links?
Thanks
Share Improve this question asked Dec 6, 2013 at 10:08 Got The Fever MediaGot The Fever Media 7601 gold badge9 silver badges28 bronze badges 3-
You know you can delegate click events to the
document
and add one singe listener for the entire application? – David Hellsing Commented Dec 6, 2013 at 10:18 - no I didn't know that... how does that work? – Got The Fever Media Commented Dec 6, 2013 at 10:19
-
1
$(document).click(function(e) { do something with e.target }
learn.jquery./events/event-delegation – David Hellsing Commented Dec 6, 2013 at 10:25
1 Answer
Reset to default 9Of course you can use AngularJs for just routing.
Where angular and jquery stop working together is, you cant use jquery selection on views generated in angular directives, because jquery do not select the runtime generated views.
To monitor for angularjs to when it finishes the DOM manipulation, use this in your controller of angular to call the jqueryStartWork
method, which should initiate working of jquery.
function stuffController($scope) {
$scope.$on('$viewContentLoaded', jqueryStartWork);
}
For some angularjs directives rendering monitor,you can have a directive which will trigger event when the angular finishes dom manipulation by that directive. For example to monitor the ng-repeat, for rendering finish add this directive
var app = angular.module('myapp', [])
.directive('onFinishRender', function ($timeout) {
return {
restrict: 'A',
link: function (scope, element, attr) {
if (scope.$last === true) {
$timeout(function () {
scope.$emit('ngRepeatFinished');
});
}
}
}
});
and then add a new function in controller which will be called on ng-repeat finishes, like this.
$scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
//you also get the actual event object
//do stuff, execute functions -- whatever...
alert("ng-repeat finished");
});
but dont forget to add this directive to your ng-repeat tag, like this
<tr data-ng-repeat="product in products" on-finish-render>
[EDIT] Today I have been trying to run a simple D3 svg append script in a div of a md-tab. I faced the problem to draw it after the controller has finished (md-tab has also finished) rendering everything. I e up that if you use the $timeout (wrapper on windows.setTimeout) it will add a new event to browser's event queue. It will run after current queue(rendering) and before the new timeout event function(s) you add in order. It will work with 0ms too.
$timeout(drawTree, 0); //drawTree is a function to get #tree div in a md-tab and append a D3 svg
本文标签: javascriptUse AngularJS just for routing purposesStack Overflow
版权声明:本文标题:javascript - Use AngularJS just for routing purposes - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742088913a2420147.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论