0

Consider I have controller and input with ngModel .

I want to get this ngModel in the controller and execute $setViewValue on it .

The snippet is attach . The trying to execute - $setViewValue gives error -

TypeError: Cannot set property '$setViewValue' of undefined

var myAppModule = angular.module('myApp', []); 
	    myAppModule.controller('myCtrl',function ($scope) {
	    			$scope.entityMail.$setViewValue("[email protected]");
	    });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js"></script>
<div ng-app="myApp">
		<form ng-controller="myCtrl">
        	Mail <input type="text" ng-model="entityMail">
    	</form>
	</div>

How to get this ngModel correctly and execute $setViewValue on it ?

Edit:

I edited my code . I must use it with $setViewValue.

6
  • Why are you setting a 3 second interval ? entityMail is undefined because it doesn't have any value yet. Commented Feb 9, 2015 at 14:10
  • See my update please . How to change it even if there is no value yet ? Commented Feb 9, 2015 at 14:15
  • Why must you use $setViewValue ? Just setting the values, $scope.entityMail = 'something' will be reflected in the view Commented Feb 9, 2015 at 14:17
  • why not just use $scope.entityMail = "[email protected]"; ? Commented Feb 9, 2015 at 14:17
  • @Tom : I just wanted to simplify my code , in the real code I have ngModel to directive and I must use setViewValue . Commented Feb 9, 2015 at 14:28

3 Answers 3

2
var myAppModule = angular.module('myApp', []); 
    myAppModule.controller('myCtrl',function ($scope,$interval) {
            $scope.entityMail  = "[email protected]";
    });
Sign up to request clarification or add additional context in comments.

Comments

1

First you have to declared $scope.entityMail as a object, then only you can apply $setViewValue in this.

var myAppModule = angular.module('myApp', []); 
        myAppModule.controller('myCtrl',function ($scope) {
                    $scope.entityMail = {};
                    $scope.entityMail.$setViewValue("[email protected]");
        });

Comments

0

Instead of $setViewValue you can directly assign the value to the ngModel

$scope.entityMail  = "[email protected]";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.