How can I dynamically add a directive in AngularJS?

You have a lot of pointless jQuery in there, but the $compile service is actually super simple in this case: .directive( ‘test’, function ( $compile ) { return { restrict: ‘E’, scope: { text: ‘@’ }, template: ‘<p ng-click=”add()”>{{text}}</p>’, controller: function ( $scope, $element ) { $scope.add = function () { var el = $compile( … Read more

CORS with spring-boot and angularjs not working

import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @Component public class SimpleCORSFilter implements Filter { private final Logger log = LoggerFactory.getLogger(SimpleCORSFilter.class); public SimpleCORSFilter() { log.info(“SimpleCORSFilter init”); } @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException … Read more

How do I use $rootScope in Angular to store variables?

Variables set at the root-scope are available to the controller scope via prototypical inheritance. Here is a modified version of @Nitish’s demo that shows the relationship a bit clearer: http://jsfiddle.net/TmPk5/6/ Notice that the rootScope’s variable is set when the module initializes, and then each of the inherited scope’s get their own copy which can be … Read more

Show spinner GIF during an $http request in AngularJS?

This really depends on your specific use case, but a simple way would follow a pattern like this: .controller(‘MainCtrl’, function ( $scope, myService ) { $scope.loading = true; myService.get().then( function ( response ) { $scope.items = response.data; }, function ( response ) { // TODO: handle the error somehow }).finally(function() { // called no matter … Read more

AngularJS – UI-router – How to configure dynamic views

There is a plunker showing how we can configure the views dynamically. The updated version of the .run() would be like this: app.run([‘$q’, ‘$rootScope’, ‘$state’, ‘$http’, function ($q, $rootScope, $state, $http) { $http.get(“myJson.json”) .success(function(data) { angular.forEach(data, function (value, key) { var state = { “url”: value.url, “parent” : value.parent, “abstract”: value.abstract, “views”: {} }; // … Read more

AngularJS: How to make angular load script inside ng-include?

The accepted answer won’t work from 1.2.0-rc1+ (Github issue). Here’s a quick fix created by endorama: /*global angular */ (function (ng) { ‘use strict’; var app = ng.module(‘ngLoadScript’, []); app.directive(‘script’, function() { return { restrict: ‘E’, scope: false, link: function(scope, elem, attr) { if (attr.type === ‘text/javascript-lazy’) { var code = elem.text(); var f = … Read more