[ACCEPTED]-Declare multiple values in ng-init-angularjs

Accepted answer
Score: 146

Use a function, way more readable:

ng-init="init()"

And:

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

Or, if 2 you must, separate inline variables with 1 a semi-colon:

ng-init="a = 1; b = 2"
Score: 40

Sometimes its not ideal to put the variables 6 in a function. For example your backend 5 is in express and you are rendering a file 4 using jade.

With Express.js you can send 3 local variables to the html. Angular can 2 accept these variables with

ng-init=""

Using ";" one 1 can have multiple variables

Example

ng-init=" hello='world'; john='doe' "  
Score: 8

I prefer wrap with object init variable:

<div ng-init="initObject = {initParam: 'initParamValue', anotherOne: 'value'}"></div>

0

Score: 3
 <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; };

  });

0

More Related questions