简体   繁体   中英

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)

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);
  });
}

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);
}

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 !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM