Declare multiple values โ€‹โ€‹in ng-init

So, I am wondering how I can declare multiple values โ€‹โ€‹inside one ng-init without creating some weird hash, then I need to always have access to the specific.

so basically i would like

<div ng-init="a = 1, b = 2">{{a}}</div> 

and I say that I would like not to do

 <div ng-init="unecessary_bs = {a: 1, b: 2}">{{unecessary_bs.a}}</div> 

However reasonable:

 <div ng-init="a = 1, b = 2">{{a}}</div> 

doesn't seem to work.

Expected thanks

+73
javascript angularjs
May 28 '14 at 19:03
source share
4 answers

Use a function that is more readable:

 ng-init="init()" 

and

 $scope.init = function() { $scope.a = 1; $scope.b = 2; } 

Or, if necessary, separate the built-in variables with a colon:

 ng-init="a = 1; b = 2" 
+116
May 28 '14 at 19:05
source share

Sometimes it is not ideal to put variables in a function. For example, your backend is in express, and you make a file using jade.

With Express.js, you can send local variables to html. Angular can take these variables with

 ng-init="" 

Using ";" can have several variables

Example

 ng-init=" hello='world'; john='doe' " 
+30
Feb 01 '15 at 1:09
source share
  <div ng-app="app"> <div ng-controller="TodoCtrl"> <ul> <li ng-repeat="todo in todos" ng-init='initTodo = init(todo)'> <span class="done-{{todo.done}}">{{todo.text}} | and todo.text via init:{{initTodo}}</span> </li> </ul> </div> 

AND

  var modulse = angular.module("app",[]); modulse.controller("TodoCtrl", function ($scope) { $scope.todos = [ {text:'todo1'}, {text:'todo2'}]; $scope.init = function (todo) { return todo.text; }; }); 
+3
Nov 06 '15 at 11:13
source share

I prefer a wrapper with an init object variable:

 <div ng-init="initObject = {initParam: 'initParamValue', anotherOne: 'value'}"></div> 
+3
May 12 '16 at 19:05
source share



All Articles