javascript-framework

using setInterval in angularjs factory

£可爱£侵袭症+ 提交于 2019-11-26 17:49:47
I was trying the code given in angularjs docs (given here: http://jsfiddle.net/zGqB8/ ) It just implements a time factory and uses $timeout to update the time object after each second. angular.module('timeApp', []) .factory('time', function($timeout) { var time = {}; (function tick () { time.now = new Date().toString(); $timeout(tick, 1000); // how to do it using setInterval() ? })(); return time; }); How would I do it using setInterval() function instead of $timeout() ? I know that one need to use scope.$apply() to enter the angular execution context but how would that work in a factory

Angular JS break ForEach

末鹿安然 提交于 2019-11-26 17:09:46
I have an angular foreach loop and i want to break from loop if i match a value. The following code does not work. angular.forEach([0,1,2], function(count){ if(count == 1){ break; } }); How can i get this? There's no way to do this. See https://github.com/angular/angular.js/issues/263 . Depending on what you're doing you can use a boolean to just not going into the body of the loop. Something like: var keepGoing = true; angular.forEach([0,1,2], function(count){ if(keepGoing) { if(count == 1){ keepGoing = false; } } }); Nishchit Dhanani The angular.forEach loop can't break on a condition match.

Computed properties in Backbone

北慕城南 提交于 2019-11-26 16:28:46
问题 I have a scenario where the data being manipulated on the client is presented and interacted with in a different way than it is represented on the server. Consider the following event resource returned from the server. { "id": 123, "start_at": 1331336004906, "end_at": 1331337704906 } And the following template for editing: <form> <!-- Notice how date and time are separated in the interface --> <input type="text" name="start_date" value="{{start_date}}" /> <input type="text" name="start_time"

Client-side Javascript app - url routing with no hash tag

时间秒杀一切 提交于 2019-11-26 15:36:19
问题 I'm working on a new client-side only app with the latest version of Ember.js. There is a single PHP page which builds the scripts, css, template files, etc. and delivers it all into index.php. I'm using an htaccess directive so that all requests are rewritten to /index.php. The PHP is only there to conveniently package the Javascript, as far as I'm concerned. Currently, routes in the browser look like this and work just fine. /#/about /#/favorites /#/etc /#/posts/5/edit However, I would like

AngularJs ReferenceError: $http is not defined

情到浓时终转凉″ 提交于 2019-11-26 12:38:07
问题 I have the following Angular function: $scope.updateStatus = function(user) { $http({ url: user.update_path, method: \"POST\", data: {user_id: user.id, draft: true} }); }; But whenever this function is called, I am getting ReferenceError: $http is not defined in my console. Can someone help me understanding what I am doing wrong here? 回答1: Probably you haven't injected $http service to your controller. There are several ways of doing that. Please read this reference about DI. Then it gets

Accessibility and all these JavaScript frameworks

 ̄綄美尐妖づ 提交于 2019-11-26 11:48:28
问题 I\'ve been investigating a few of the JavaScript frameworks such as Backbone.js and Batman.js for a while and whilst I really like them, I have one niggling thing that I keep coming back to. That issue is accessibility. As a web developer I\'ve always tried to make my websites and applications with accessibility in mind, especially using the idea of progressive enhancement. Clearly out of the box these new JS frameworks don\'t gracefully degrade, so I was wondering what are other developers

Pass variables to AngularJS controller, best practice?

老子叫甜甜 提交于 2019-11-26 10:10:19
问题 I am brand new to AngularJS and like what I\'ve seen so far, especially the model / view binding. I\'d like to make use of that to construct a simple \"add to basket\" piece of functionality. This is my controller so far: function BasketController($scope) { $scope.products = []; $scope.AddToBasket = function (Id, name, price, image) { ... }; } And this is my HTML: <a ng-click=\"AddToBasket(\'237\', \'Laptop\', \'499.95\', \'237.png\')\">Add to basket</a> Now this works but I highly doubt this

jQuery: Check if div with certain class name exists

别来无恙 提交于 2019-11-26 10:05:55
问题 Using jQuery I\'m programmatically generating a bunch of div \'s like this: <div class=\"mydivclass\" id=\"myid1\">Some Text1</div> <div class=\"mydivclass\" id=\"myid2\">Some Text2</div> Somewhere else in my code I need to detect if these DIVs exist. The class name for the divs is the same but the ID changes for each div. Any idea how to detect them using jQuery? 回答1: You can simplify this by checking the first object that is returned from JQuery like so: if ($(".mydivclass")[0]){ // Do

Ways to save Backbone.js model data?

拥有回忆 提交于 2019-11-26 07:52:09
问题 I am more into front end development and have recently started exploring Backbone.js into my app. I want to persist the model data to the server. Could you please explain me the various way to save the Model data (using json format). I am using Java on server side. Also I have mainly seen REST being used to save data. As i am more into front end dev, i am not aware of REST and other similar stuff. It would be great if someone could please explain me the process with some simple example. 回答1:

Angular JS break ForEach

最后都变了- 提交于 2019-11-26 06:04:54
问题 I have an angular foreach loop and i want to break from loop if i match a value. The following code does not work. angular.forEach([0,1,2], function(count){ if(count == 1){ break; } }); How can i get this? 回答1: There's no way to do this. See https://github.com/angular/angular.js/issues/263. Depending on what you're doing you can use a boolean to just not going into the body of the loop. Something like: var keepGoing = true; angular.forEach([0,1,2], function(count){ if(keepGoing) { if(count ==