smallseo.info

angular.js

HTML enhanced for web apps AngularJS — Superheroic JavaScript MVW Framework angularjs is what html would have been, had it been designed for building web-apps. declarative templates with data-binding, mvw, mvvm, mvc, dependency injection and great testability story all implemented with pure client-side javascript!

Service vs provider vs factory

What are the differences between AngularJS module's Service, Provider and Factory?


Source: (StackOverflow)

Databinding in AngularJS

How does databinding work in the AngularJS framework?

I haven't found technical details on their site. It's more or less clear how it works when data is propagated from view to model. But how does AngularJS track changes of model properties without setters and getters? I found that there are JavaScript watchers that may do this work. But they are not supported in Internet Explorer 6 and Internet Explorer 7. So how does AngularJS know that I changed for example:

myobject.myproperty="new value";

and reflect this change on a view?


Source: (StackOverflow)

What is the best way to conditionally apply a class?

Lets say you have an array that is rendered in a ul with an li for each element and a property on the controller called selectedIndex. What would be the best way to add a class to the li with the index selectedIndex in AngularJS?

I am currently duplicating (by hand) the li code and adding the class to one of the li tags and using ng-show and ng-hide to show only one li per index.


Source: (StackOverflow)

angular.service vs angular.factory

I have seen both angular.factory() and angular.service() used to declare services; however, I cannot find angular.service anywhere in official documentation.

What is the difference between the two methods? Which should be used for what (assuming they do different things)?


Source: (StackOverflow)

What are the nuances of scope prototypal / prototypical inheritance in AngularJS?

The API Reference Scope page says:

A scope can inherit from a parent scope.

The Developer Guide Scope page says:

A scope (prototypically) inherits properties from its parent scope.

So, does a child scope always prototypically inherit from its parent scope? Are there exceptions? When it does inherit, is it always normal JavaScript prototypal inheritance?


Source: (StackOverflow)

How do search engines deal with AngularJS applications?

I see two issues with AngularJS application regarding search engines and SEO:

1) What happens with custom tags? Do search engines ignore the whole content within those tags? i.e. suppose I have

<custom>
  <h1>Hey, this title is important</h1>
</custom>

would <h1> be indexed despite being inside custom tags?


2) Is there a way to avoid search engines of indexing {{}} binds literally? i.e.

<h2>{{title}}</h2>

I know I could do something like

<h2 ng-bind="title"></h2>

but what if I want to actually let the crawler "see" the title? Is server-side rendering the only solution?


Source: (StackOverflow)

How to set focus on input field?

What is the 'Angular way' to set focus on input field in AngularJS?

More specific requirements:

  1. When a Modal is opened, set focus on a predefined <input> inside this Modal.
  2. Everytime <input> becomes visible (e.g. by clicking some button), set focus on it.

I tried to achieve the first requirement with autofocus, but this works only when the Modal is opened for the first time, and only in certain browsers (e.g. in Firefox it doesn't work).

Any help will be appreciated.


Source: (StackOverflow)

Difference between angular-route and angular-ui-router

I am new to AngularJS. I find Angular quite interesting and planning to use angular in my big apps. So I am in the process to find out the right modules to use.

What is the difference between ngRoute (angular-route.js) and ui-router (angular-ui-router.js) modules?

In many articles when ngRoute is used, route is configured with $routeProvider. However, when used with ui-router, route is configured with $stateProvider and $urlRouterProvider.

This creates a bit of confusion for me. Which module should I use for better manageability and extensibility? Your answers are greatly appreciated.


Source: (StackOverflow)

How to set the value property in AngularJS' ng-options?

Here is what seems to be bothering a lot of people (including me). When using the ng-options directive in AngluarJS to fill in the options for a <select> tag I cannot figure out how to set the value for an option. The documentation for this is really unclear - at least for a simpleton like me.

I can set the text for an option easily like so:

ng-options="select p.text for p in resultOptions"

when resultOptions is for example:

[
    {
        "value": 1,
        "text": "1st"
    },
    {
        "value": 2,
        "text": "2nd"
    }
]

Should be (and probably is) the most simple thing to set the option values, but so far I just don't get it.


Source: (StackOverflow)

Insert HTML into view using AngularJS

Is it possible to create an HTML fragment in an Angular controller and have this HTML shown in the view?

This comes from a requirement to turn an inconsistent JSON blob into a nested list of id : value pairs. Therefore the HTML is created in the controller and I am now looking to display it.

I have created a model property, but cannot render this in the view without it just printing the HTML.


Update

It appears that the problem arises from angular rendering the created HTML as a string within quotes. Will attempt to find a way around this.

Example controller :

var SomeController = function () {

    this.customHtml = '<ul><li>render me please</li></ul>';
}

Example view :

<div ng:bind="customHtml"></div>

Gives :

<div>
    "<ul><li>render me please</li></ul>"
</div>

Source: (StackOverflow)

Can one controller call another?

Is it possible to have one controller use another?

For example:

This HTML document simply prints a message delivered by the MessageCtrl controller in the messageCtrl.js file.

<html xmlns:ng="http://angularjs.org/">
<head>
    <meta charset="utf-8" />
    <title>Inter Controller Communication</title>
</head>
<body>
    <div ng:controller="MessageCtrl">
        <p>{{message}}</p>
    </div>

    <!-- Angular Scripts -->
    <script src="http://code.angularjs.org/angular-0.9.19.js" ng:autobind></script>
    <script src="js/messageCtrl.js" type="text/javascript"></script>
</body>
</html>

The controller file contains the following code:

function MessageCtrl()
{
    this.message = function() { 
        return "The current date is: " + new Date().toString(); 
    };
}

Which simply prints the current date;

If I were to add another controller, DateCtrl which handed the date in a specific format back to MessageCtrl, how would one go about doing this? The DI framework seems to be concerned with XmlHttpRequests and accessing services.

Can anyone help?


Source: (StackOverflow)

How can AngularJS bind to list of checkbox values?

I have a few checkboxes:

<input type='checkbox' value="apple" checked>
<input type='checkbox' value="orange">
<input type='checkbox' value="pear" checked>
<input type='checkbox' value="naartjie">

That I would like to bind to a list in my controller such that whenever a checkbox is changed the controller maintains a list of all the checked values e.g. ['apple', 'pear'].

ng-model seems to only be able to bind the value of one single checkbox to a variable in the controller.

Is there another way to do it so that I can bind the four checkboxes to a list in the contrller?


Source: (StackOverflow)

What's the correct way to communicate between controllers in AngularJS?

What's the correct way to communicate between controllers in AngularJS?

I'm currently using a horrible fudge involving 'window':

function StockSubgroupCtrl($scope, $http) {
    $scope.subgroups = [];
    $scope.handleSubgroupsLoaded = function(data, status) {
        $scope.subgroups = data;
    }
    $scope.fetch = function(prod_grp) {
        $http.get('/api/stock/groups/' + prod_grp + '/subgroups/').success($scope.handleSubgroupsLoaded);
    }
    window.fetchStockSubgroups = $scope.fetch;
}

function StockGroupCtrl($scope, $http) {
    ...
    $scope.select = function(prod_grp) {
        $scope.selectedGroup = prod_grp;
        window.fetchStockSubgroups(prod_grp);
    }
}

Source: (StackOverflow)

Why does angularjs include an empty option in select

I've been working with angular for the last few weeks and the one thing which is really bothering me is that even after trying all permutations or the configuration defined in the spec at http://docs.angularjs.org/api/ng.directive:select, I still get an empty option as the first child of select element.

Here's the Jade

select.span9(ng-model='form.type', required, ng-options='option.value as option.name for option in typeOptions');

Here the controller

$scope.typeOptions = [
    { name: 'Feature', value: 'feature' }, 
    { name: 'Bug', value: 'bug' }, 
    { name: 'Enhancement', value: 'enhancement' }
];

Finally, here's the HTML which gets generated

<select ng-model="form.type" required="required" ng-options="option.value as option.name for option in typeOptions" class="span9 ng-pristine ng-invalid ng-invalid-required">
    <option value="?" selected="selected"></option>
    <option value="0">Feature</option>
    <option value="1">Bug</option>
    <option value="2">Enhancement</option>
</select>

What do I need to do to get rid of it?

P.S. Things work without this as well, but it just looks odd if you use select2 without multiple selection.


Source: (StackOverflow)

Where to put model data and behaviour?

I am working with AngularJS for my latest project. In the documentation and tutorials all model data is put into the controller scope. I understand that is has to be there to be available for the controller and thus within the corresponding views.

However I dont think the model should actually be implemented there. It might be complex and have private attributes for example. Furthermore one might want to reuse it in another context/app. Putting everything into the controller totally breaks MVC pattern.

The same holds true for the behaviour of any model. If I would use DCI architecture and separate behaviour from the data model, I would have to introduce additional objects to hold the behaviour. This would be done by introducing roles and contexts.

Of course model data and behaviour could be implemented with plain javascript objects or any "class" pattern. But what would be the AngularJS way to do it? Using services?

So it comes down to this question:

How do you implement models decoupled from the controller, following AngularJS best practices?


Source: (StackOverflow)