Questions tagged [angularjs-ng-init]

In AngularJS, the ngInit directive allows you to evaluate an expression in the current scope.

From Angularjs documentation:

The ngInit directive allows you to evaluate an expression in the current scope.
The only appropriate use of ngInit is for aliasing special properties of ngRepeat. Besides this case, you should use controllers rather than ngInit to initialize values on a scope.

Note: If you have assignment in ngInit along with $filter, make sure you have parenthesis for correct precedence:

<div ng-init="test1 = (data | orderBy:'name')"></div>

Usage

as attribute:

<ANY
  ng-init="expression">
...
</ANY>

as CSS class:

<ANY class="ng-init: expression;"> ... </ANY>
133 questions
52
votes
4 answers

Pass parameter to Angular ng-include

I am trying to display a binary tree of elements, which I go through recursively with ng-include. What is the difference between ng-init="item = item.left" and ng-repeat="item in item.left" ? In this example it behaves exactly the same, but I use…
41
votes
8 answers

Angular passing scope to ng-include

I have a controller that I wrote that I use in multiple places in my app with ng-include and ng-repeat, like this:
In the controller/template, I…
18
votes
3 answers

Set Default Value to ng-bind in HTML

I'd like to set a default value to scope, which is picked up by ng-bind. I am doing this like: In this example, the span is set to innerHTML =…
Kohjah Breese
  • 3,619
  • 5
  • 29
  • 43
16
votes
3 answers

AngularJS: scope of ng-init

What is the scope of ng-init in Angular? Here's a simple example that demonstrates my question:
2012 | 2013
Johnny Oshika
  • 45,610
  • 33
  • 151
  • 234
15
votes
1 answer

setting Angular select ng-init to first value in array

I have a simple select element that I am trying to enforce a value being present to submit the form and I have tried both setting the required attribute as well as using ng-init to ensure there is a value selected and both fail. I am using…
simplesthing
  • 1,976
  • 3
  • 12
  • 16
13
votes
2 answers

AngularJS ngInit with Date

I'm coding in .NET MVC and would like to pass a stored DateTime object to the front-end as an initial value for a Date object in a controller. Here's what I've tried: ng-init="params.datetime = new Date()" This doesn't seem to work, and I'm getting…
Dzulqarnain Nasir
  • 2,030
  • 1
  • 19
  • 16
12
votes
2 answers

Angular : ng-init does not run on load

I have seen a few exmaples on stack overflow about this ng-init issue, although I cant seem to find one which references it with the use of a controller. I have called the function in the controller by having the following in the html file
Anton Hughes
  • 1,387
  • 3
  • 17
  • 30
12
votes
2 answers

Ng-init via function

As you know, it is possible to initialize objects as follows:
Asqan
  • 3,858
  • 10
  • 52
  • 90
7
votes
1 answer

Calling a init function when a directive is loaded

I have a simple directive that loads comments from a service(commentsService): 'use strict'; angular.module('mean.rank') .directive('commentList', function(CommentsService, Global) { return { restrict: 'E', …
Itsik Mauyhas
  • 3,163
  • 7
  • 48
  • 93
7
votes
1 answer

Does ng-init watch over change on instantiated property like ng-model does?

Does ng-init watch over change on instantiated property like ng-model does? Apparently not, so I set a watch as shown below: app.js var app = angular.module('plunker', []); app.controller('MainCtrl', function($scope) { $scope.$watch('myProp1',…
6
votes
4 answers

AngularJS select: remove empty option and use data objects not arrays

I've been searching high and low for an explanation as to how to remove the empty option AngularJS always renders in selects. I have found lots of information doing this when the options are derived from a JSON array placed directly in the code,…
5
votes
1 answer

ng-repeat splice showing double

For some reason, when I splice an object into my ng-repeat array, it doubles what I splice in and hides the last object in the array. However, if I click the toggle hide and "refresh" the ng-repeat, it shows the right data. Does anyone know why this…
bryan
  • 7,656
  • 12
  • 65
  • 136
5
votes
3 answers

How to $setdirty to a single input

I've got a little problem. I want to set to dirty a single input, I mean, because when I give a value automatically it stays in pristine class, doesn't change, and doesn't save the new value. If I edit it, it works and change the class. I want to…
5
votes
3 answers

Add filters in ng-init

{{ hf.name }}
IOR88
  • 1,264
  • 3
  • 17
  • 31
5
votes
1 answer

Multiple ng-init scope issues

I'm trying to use ng-include with ng-init to re-use the same component by only changing its data. The component code ("slider.html", which has no controller) looks like this:
{{person.name}}
From the…
Dario
  • 5,440
  • 8
  • 31
  • 39
1
2 3
8 9