Ali-Alrabi
Ali-Alrabi

Reputation: 1698

get ng-init value from scope

I want to get ng-init value from angular scope

controller

$scope.data={}
$scope.initvalue="myvalue";

Html

<input type="text" ng-model="data.value" ng-init="data.value= initvalue">

I want to set initvalue in input box and sent it to controller by ng-model(I want to able to modify this values so I need to make that)

Upvotes: 1

Views: 7162

Answers (3)

Jenson Raby
Jenson Raby

Reputation: 789

In html

<input type="text" ng-model="data.value" ng-init="setvalue()">

In controller

 $scope.data ={}
    $scope.initvalue="myvalue";
    $scope.setvalue= function(){
        $scope.data.value = $scope.initvalue;
    }

This may help for you !

Upvotes: -1

Bridgit Thomas
Bridgit Thomas

Reputation: 343

You can try using using $watch. Please try this demo eg :

<div ng-controller="yourController" >
    <input type="text" id="demoInput" ng-model="demoInput" ng-init="demoInput='value'" />
</div>

code in controller

var yourController = function ($scope) {
 console.log('demo');
 $scope.$watch("demoInput", function(){
    console.log($scope.demoInput);
 },1000);
}

Upvotes: 0

joe-re
joe-re

Reputation: 78

Does this make sense?

http://jsfiddle.net/c7bsrenu/2/

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
  $scope.data={}
  $scope.initvalue="myvalue";
  $scope.$watch('data.value', function(newVal, oldVal) {
      console.log(newVal);
      console.log(oldVal);
  });
}

Upvotes: 3

Related Questions