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>
135 questions
5
votes
1 answer

correct way to initialise scope values when the view is loaded with angularjs, ngInit?

I've been learning angularJs for the past few weeks and been looking at a number of large scale apps to see how things work in the real world. In most of them I have noticed when a view is loaded : ng-init="init()" i.e. the function init() is…
Aidan Gee
  • 578
  • 1
  • 11
  • 26
4
votes
1 answer

ng-init miscalculates aliased index after array.splice

I have encountered a strange behavior related to ng-init, any help would be appreciated. I have a model object which has a flats property that is an array of flat objects. Each flat object has rooms property which is an array of room objects. I'm…
ysf
  • 4,634
  • 3
  • 27
  • 29
3
votes
2 answers

Assign ng-model an object in a select

I'm iterating over a range and populating new select options for each index. The options are not related to the range I am iterating over but are options of different types. Code as follows:
Imran NZ
  • 1,327
  • 2
  • 12
  • 22
3
votes
2 answers

AngularJS ng-init is not working in ng-repeat

I need a Sum of Balance from the Following Data in HTML not in JS Controller Function. So, I used the ng-init within the ng-repeat. But I can't able to get the result. My JSON Data is { "records":[ { "ldat":"2014-08-13", …
3
votes
2 answers

AngularJS - Accessing ng-init variables from run method

1) I have variables initialized in ng-init Eg - ng-init="password='Mightybear'"; 2) I want to access it from the .run method. Eg - anguar.module("ngApp", []) .run(function() { //Access password here }); Below scenarios I have tried, and did…
3
votes
2 answers

How to initialize a select value when option is in ng-repeat?

At first I tried
Jon Harding
  • 4,928
  • 13
  • 51
  • 96
3
votes
5 answers

ng-init json Object

I use angularjs (ng-init) and I want to assign value to variable as jsonObj. I try this one but it doesn't work. ng-init="percentObj = [{ "value":40,"color":"#F5A623" },{ "value":60,"color":"#F5A623" }]; and another question I want to assign value…
user3900567
  • 31
  • 1
  • 1
  • 3
3
votes
1 answer

AngularJS - reset ngInit in ngRepeat

Assume the following JSON structure, which is strongly simplified from what I am currently working with: $scope.var1 = { "test1": { "name": "test1", "belongsTo": "var1", "additionalInformation": "blabla" }, …
meberhard
  • 1,797
  • 1
  • 19
  • 24
3
votes
2 answers

Passing variable from jade to ng-init not working

I'm trying to pass an object from jade to ng-init in angular This: doesn't work: ng-init='tables=!{JSON.stringify(tables)}' This: expands but, ng-init='tables=#{JSON.stringify(tables)}' the output is unescaped and filled with…
laggingreflex
  • 32,948
  • 35
  • 141
  • 196
3
votes
1 answer

Is using ng-init for bind once valid in angular js?

i have a collection of values whose structure lets assume to be var a = [{id:1, value:12, name="one"}, {id:2, value:34, name="two"},...] i wanted to display this in a series of controls so that user can change the values. but with that i also…
Parv Sharma
  • 12,581
  • 4
  • 48
  • 80
2
votes
2 answers

AngularJS revert selected value of control nested in ng-repeat on dialog cancel

I want to revert a select tag value that is inside a ng-repeat after cancelling a confirm dialog. Here is what I have so far: Relevant HTML: