Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass scope variable in ng-init using AngularJS

I have problem passing the variable declared from the scope to ng-init:

so far I have something like this:

$scope.x = '10';

<div ng-controller = "controller" ng-init="function(x)">

How do I pass x var from the scope inside ng-init function?

like image 303
Lulzim Avatar asked Feb 24 '26 23:02

Lulzim


2 Answers

A lot of people will tell you you shouldn't do this (as mentioned in the docs here: https://docs.angularjs.org/api/ng/directive/ngInit).

As for actually doing it....

<div ng-controller="controller" ng-init="x = x">
like image 133
Mike Robinson Avatar answered Feb 26 '26 12:02

Mike Robinson


Maybe this one can help you:

$scope.x = '10';

<div ng-controller = "controller" ng-init="x">
like image 36
Lee Lee Avatar answered Feb 26 '26 11:02

Lee Lee