javascript - Accessing pre-filled input fields in controllers -
what best way access pre-filled input fields values in angularjs? example, how can access value of variable1 (which pre-filled on pageload) within controller scope? using ng-model directive on hidden input field , using $scope.formdata.variable1 access in controller no luck. tried searching in web similar issue, of examples related accessing form field on submit or click. here link plnkr
<body ng-controller="maincontroller"> <h1>{{message}}</h1> <form name="formdata"> <input type="hidden" name="variable1" ng-model="formdata.variable1" value="abc" /> </form>
angularjs code:
(function() { var app = angular.module("testapp", []); var maincontroller = function($scope) { console.log($scope.formdata.variable1); $scope.message = "hello, angular!"; }; app.controller("maincontroller", ["$scope", maincontroller]); }());
you can this. using ng-init;
<input name="data[name]" ng-model="data.name" ng-init="data.name='mark'" />
or
you can use directive. this.
app .directive('ngdefaultvalue', function() { return { restrict: 'a', controller: function($scope, $element, $attrs, $parse) { $parse($attrs.ngmodel).assign($scope, $attrs.ngdefaultvalue || $attrs.value); } }; });
Comments
Post a Comment