Add ng-click dynamically in directive link function

Better Solution (New): After reading through the Angular docs I came across this: You can specify template as a string representing the template or as a function which takes two arguments tElement and tAttrs (described in the compile function api below) and returns a string value representing the template. So my new directive looks like … Read more

Defer angularjs watch execution after $digest (raising DOM event)

$timeout is normally what is used to run something after a digest cycle (and after the browser renders). $timeout will cause another digest cycle to be executed after the function is executed. If your trigger does not affect anything Angular, you can set the invokeApply argument to false to avoid running another digest cycle. If … Read more

AngularJS : minification issue in directive

You need to declare a controller as follows: controller: [‘$scope’, function ($scope) { $scope.test = 3; }] Full example here: angular.module(‘person.directives’). directive(“person”, [‘$dialog’, function($dialog) { return { restrict: “E”, templateUrl: “person/views/person.html”, replace: true, scope: { myPerson: ‘=’ }, controller: [‘$scope’, function ($scope) { $scope.test = 3; }] } }]); A solution provided by @Sam would … Read more

Error: $digest already in progress

Duplicated: Prevent error $digest already in progress when calling $scope.$apply() That error you are getting means Angular’s dirty checking is already in progress. Most recent best practices say that we should use $timeout if we want to execute any code in the next digest iteration: $timeout(function() { // the code you want to run in … Read more

How are the attribute prefixes “x-” and “data-” used in AngularJS

The HTML5 spec allows for arbitrary attributes as long as they’re prefixed with data- like so: <div data-myattribute=””></div> Whereas this would be invalid HTML5: <div myattrbute=””></div> For more information on data- attributes, have a look here. As for “x-” attributes, I think you mean “x:” attributes and elements, which are specific to XHTML validation… To … Read more

Differences between returning an object vs a function in a directive definition?

Returning only a function in a directive is just a shorthand for the link function in the full definition. If you are specifying something other than a link function (like templateUrl) then you need to write it the long way: angular.module(“app”). directive(“widgetUno”, [“$http”, function ($http) { return { link: function(scope, element, attrs) { // A … Read more

how to call service method from ng-change of select in angularjs?

You have at least two issues in your code: ng-change=”getScoreData(Score) Angular doesn’t see getScoreData method that refers to defined service getScoreData: function (Score, callback) We don’t need to use callback since GET returns promise. Use then instead. Here is a working example (I used random address only for simulation): HTML <select ng-model=”score” ng-change=”getScoreData(score)” ng-options=”score as … Read more

AngularJS number input formatted view

As written in the comments, input type=”number” doesn’t support anything but digits, a decimal separator (usually , or . depending on the locale) and – or e. You may still enter whatever you want, but the browser will discard any unknown / incorrect character. This leaves you with 2 options: Use type=”text” and pattern validation … Read more