2016-12-10 64 views
0

損失後,我可以檢查我是否有聚焦特定元素或不使用指令改變元素焦點的角度JS

ng-focus="focus=true" ng-blur="focus=false" 

但事情是,我想進行自定義指令,使得它會在重點丟失後改變我的元素模型,所以我認爲我不能使用ng-focusng-blur。 好吧,讓我告訴你,正是我想要的 假設我有一個文本框 ,如果用戶輸入10和tab所以,失去焦點的後我的文本框必須返回currrent月份和年份 對於例如,如果用戶輸入

10 

輸出應爲10/12/2016

Thanku

+0

不輸入字段是一個日期選擇器? –

+0

然後你可以使用ng-change – GraveyardQueen

回答

0

看一看與NG-變化NG-模型選項

http://www.w3schools.com/angular/ng_ng-model-options.asp

https://docs.angularjs.org/api/ng/directive/ngModelOptions

這是一個基本plnkr你可以擴大: https://plnkr.co/edit/crkrhOic70dYjViuUh6x?p=preview

app.component('myComponent', { 
    bindings: { 
    obj: '<' 
    }, 
    template: '<input type="text" ng-model="$ctrl.someDate" ng-model-options="{updateOn: \'blur\'}" ng-change="$ctrl.change()"/>', 
    controller: function MyComponentCtrlx() { 

    this.change =()=>{ 
     this.someDate += '/12/2016'; 
    } 
    } 
}); 
+0

先生,我認爲這不會幫助我的diredctive必須是這樣的屬性 and my directive app.directive('changeFocusadd ',{})因爲假設對於一些輸入我想添加這個和另一個指令我應該怎麼做...? –

0

您可以使用ng-modelng-blur和特定方法(changeDate())返回您需要的時候焦點丟失:

HTML:

<input type="text" ng-model="textInput" ng-blur="changeDate()"> 

控制器:

$scope.changeDate = function() { 
    $scope.textInput = $scope.textInput + '/12/2016'; 
} 

當然,你應該添加一個更好的方式來計算日期在changeDate我做了快只是爲了給你一個想法。

編輯:對不起它NG-模糊,當你失去焦點

要使用它作爲指令:

JS:

app.directive('myInput', function() { 
    return { 
     restrict: 'E', 
     replace: 'true', 
     scope: { 
     inputdata: '=' 
     }, 
     template: '<input type="text" ng-model="inputdata" ng-blur="changeDate()">', 
     controller: function ($scope) { 
     $scope.changeDate = function() { 
      $scope.inputdata = $scope.inputdata + '/12/2016'; 
     } 
     } 
    }; 
}); 

HTML:

<my-input inputdata="textInput"></my-input> 
+0

不,我想要一個自定義指令,這個指令對於所有的控制器都是通用的。 –

+0

編輯添加指令。 –

+0

坐我認爲這不會幫助我的diredctive必須是這樣的屬性和我的指令app.directive('changeFocusadd',{}),因爲假設爲一些輸入我想添加這個和另一個指令我應該怎麼做...? –