2015-02-09 39 views
0

考慮我有控制器和輸入ngModel如何獲取控制器輸入的ngModel?

我想在控制器中得到這個ngModel並在其上執行$setViewValue

這段代碼是附加的。在試圖執行 - $setViewValue給出錯誤 -

TypeError: Cannot set property '$setViewValue' of undefined 

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

如何獲取ngModel正確,就可以執行$setViewValue

編輯:

我編輯我的代碼。我必須使用$setViewValue

+0

你爲什麼設置3秒的間隔? 'entityMail'是未定義的,因爲它沒有任何價值。 – 2015-02-09 14:10:51

+0

請參閱我的更新。即使沒有價值,如何改變它? – URL87 2015-02-09 14:15:14

+0

爲什麼*必須*您使用'$ setViewValue'?只需設置值,'$ scope.entityMail ='something''將反映在視圖 – Tom 2015-02-09 14:17:01

回答

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

請看我的更新 – URL87 2015-02-09 14:13:37

1

首先你必須聲明$ scope.entityMail作爲一個對象,那麼只有你可以在這裏申請$ setViewValue。

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