2017-04-19 25 views
0

我無法從我的name directive中檢索到template-urlng-model值。從指令模板中檢索ng模型的值到另一個Html文件

團隊two.html

<form name="userForm" novalidate> 
    <div name-directive></div> 
</form> 
<pre>{{userForm.firstname}}</pre> 

在這裏,我有name-directiveformpre標籤中查看我在input text of name-directive

名稱directive.js

鍵入值
(function() { 
'use strict'; 

var app = angular.module('app.input-form'); 

app.directive('nameDirective', function() { 
return { 
    restrict: 'AE', 
    scope: true, 
    controller: function() { 
    var vm = this; 
    vm.namePattern = /^[a-zA-Z ]{1,25}$/; 
    }, 
    controllerAs: 'vm', 
    templateUrl: '/src/client/app/input-form/name.html' 
} 
}); 
})(); 

在這裏,我有一個templateUrl不介意其他代碼因爲我認爲這與我的關注無關。

name.html

<input type="text" name="firstname" class="form-control" placeholder="Enter 
your first name" 
    ng-model="firstname" 
    ng-minlength="2" 
    ng-maxlength="20" 
    required 
    ng-pattern="vm.namePattern"/> 
在這裏

,我有一個ng-model,我要訪問的是firstname ..

編輯注:我發現,如果我添加$viewValue<pre>{{userForm.firstname.$viewValue}}</pre>它的工作..我得到我想要的..但​​我認爲這不是我的問題的確切解決方案..

編輯2添加鏈接Plunker:https://plnkr.co/edit/6glStzEq5ckZZzph2qLw?p=preview

回答

0

由於您使用該指令的controllerAs需要引用,當您訪問從HTML變量。

ng-model="vm.firstname"

​​
+0

謝謝回答。是的,那也行得通。但是我的問題出現在'pre'標籤內的'team-two.html'中,我無法訪問'name.html'的'ng-model' – Marksmanship