0
我將創建一個指令,獲取我的模型的屬性並更改此值,但是當我在指令中分配值時,它會覆蓋由異步調用檢索的整個對象: 我有我的模型對象在指令中更改NgModel屬性覆蓋所有範圍angularJs
object: {
phone:"123456",
name: "Jhon",
surname: "Smith"
}
而且我會改電話號碼,我有 我-HTML
<input type="text" id="phone" class="form-control" data-ng-model="object.phone" custom-number>
和我的指令:
.directive('customNumber', [ '$timeout', function ($timeout) {
return {
restrict: 'A',
require: 'ngModel',
scope: {
bindedModel: "=ngModel"
},
link: function(scope, element, attrs) {
scope.bindedModel= "0000";
}
}
而且我的模型對象它唯一的財產電話覆蓋,所以現在我的模式是:
object: {
phone: "0000"
}
爲什麼它覆蓋我的整個對象以及如何我避免這個?
編輯
如果我改變我的HTML在
<custom-number model="object.phone" />
我的指令:
var tpl = ' <input type="text" id="id" class="form-control" ng-model="model">';
var init = 0;
return {
restrict: 'EA',
scope: {
model: '=',
id:'='
},
template: tpl,
link: function(scope, element, attrs) {
scope.$watch('model', function(newValue, oldValue) {
if (oldValue != newValue && init == 0){
scope.model = "0000"
}
});
}
};
它改變唯一正確的價值,但我只會先做到這一點時間
你好thankyou..The問題,也許該對象值是異步,因爲輸入內部NG-重複? – LorenzoBerti
我編輯我的問題 – LorenzoBerti
我編輯我的答案後編輯 – mpdev7