2013-12-16 39 views
1

我有一個看起來像這樣的dierective:將ng-model設置爲與變量相同而不是引用該變量?

.directive('gdInputField', function() { 
    return { 
     //will only work for elements, not attributes. 
     restrict: 'E', 
     template: "<label for='{{name}}'>{{label}}</label><input name='{{key}}' id='{{name}}' type='{{type}}' ng-required='{{required}}' /><p>{{entry.1949113882}}</p>", 
     scope: { 
       label: '@label', 
       name: '@name', 
       key: "@key", 
       required: "@required", 
       type: "@type" 
     }, 
    }; 
}) 

我想補充一點,設置與@key作爲模型的輸入字段的名稱值。如果我設置ng-model='key'。我得到@key的字符串作爲ng模型的內容。

這似乎是結果:

$scope={ 
    someting: @key 
} 

我要的是:

$scope={ 
    @key: ''; 
} 

如果用戶在輸入@key應該更新寫東西。

此外,什麼是模型投標的當前名稱或我怎麼能找出?

回答

0

如下

app.directive('gdInputField', function() { 
    return { 
     //will only work for elements, not attributes. 
     restrict: 'E', 
     template: "<label for='{{name}}'>{{label}}</label><input name='{{inputName}}' ng-model='key' id='{{name}}' type='{{type}}' ng-required='{{required}}' /><p>{{key}}</p>", 
     link: function (scope, elem, attrs) { 
      scope.inputName = attrs.key; 
     }, 
     scope: { 
      label: '@label', 
      name: '@name', 
      key: "=", 
      required: "@required", 
      type: "@type" 
     }, 
    }; 
}); 

關鍵的一點是,關鍵應該是雙向的,用作模型重寫代碼。

+0

是否爲使雙向鑰匙成爲等號? – Himmators

+0

是的。等號可以使用綁定。這裏有好文章http://onehungrymind.com/angularjs-sticky-notes-pt-2-isolated-scope/ – Alborz

相關問題