2014-11-03 75 views
4

所以,我有一個表單,我需要使用自定義指令。 我需要什麼:將user模型傳遞給指令。傳遞模型屬性到自定義指令

<form> 
    <input type="text" ng-model="user.login"> 
    <input type="password" ng-model="user.password"> 

    <span ng-custom-directive ng-model="user.testfield"></span> 

</form> 

指令模板看起來是這樣的:

<span><input type="checkbox" ng-model="[HERE I NEED user.testfield TO WORK WITH user]"> </span> 

我怎樣才能通過user模型來指導模板?

表格後提交我需要user.testfield是在$scope.user喜歡可供選擇:

console.log($scope.user) 
{ 
    login: 'test', 
    password: 'test', 
    testfield: true|false 
} 

回答

8

你可以在t他其他方式plunker

簡單:

scope: { 
    bindedModel: "=ngModel" 
}, 
template: '<input type="text" ng-model="bindedModel">' 
0

好吧,我發現類似的問題,並以這種方式解決了我的問題:

angular.module("myApp") 
    .directive "ngCustomDirective",() -> 
     restrict: 'A', 

     scope: 
     field: '@', 
     model: '=' 

     template: '<span><input type="checkbox" ng-model="model[field]"></span>' 

和指導使用量將是:

<span ng-custom-directive 
     ng-bind-model="user" 
     ng-bind-field="testfield"> 
</span> 
+0

請張貼鏈接到類似的問題。謝謝! – 2014-11-03 16:54:54

+1

http://stackoverflow.com/questions/20828046/watch-ng-model-inside-directive – coldmind 2014-11-03 16:55:41

相關問題