Reputation: 425
Hello I'm having trouble with two way binding between my controller and my directive.
This is the Html
<map-brands brands="brands" focused-brands="focusedBrands" add-brand-input="addBrandInput" suggestions="suggestions" open-brand-tab="openBrandTab(data)" remove-brand="removeBrand(data)" brands-boolean="brandsBoolean" brand-arrow="brandArrow(data)" add-brand-input-change="addBrandInputChange(data)" add-the-brand="addTheBrand(data)" add-brand-iterator="addBrandIterator">
</map-brands>
This is the Directive
scope:{
"suggestions": '=?',
"brands": '=?',
"focusedBrands": '=?',
"addBrandInput": '=?',
"removeBrand": "&",
"openBrandTab": "&",
"brandsBoolean": "=?",
"brandArrow": "&",
"addBrandInputChange": "&",
"addTheBrand": "&",
"addBrandIterator":"=?"
},
templateUrl: '/views/directives/mapBrands.html'
This is the Js function with the problem:
$scope.addTheBrand = function(data){
$scope.addBrandInput = 'SomeText';
console.log("$scope.addBrandInput",$scope.addBrandInput);
};
And finally the template:
<div class="brands-add-container" ng-if="brandsBoolean">
<input type="text" class="brandsAdd" ng-keydown="brandArrow({data:$event})" ng-model="addBrandInput" placeholder="ADD BRAND(S)" ng-change="addBrandInputChange({data:addBrandInput})" />
<span class="listOfSuggestions" ng-if="addBrandInput != '' ">
<span class="suggestion" ng-repeat="(key, value) in suggestions" ng-click="addTheBrand({data:value})" ng-class="{'activeBrand' : addBrandIterator == key}"> {{value}} </span>
</span>
</div>
The problem is when i type some text in the INPUT and afterwards i click the ng-click (located in the last span), The input text is not changing according to the addTheBrand function.
---In the console.log i have placed the addBrandInput is changed to "someText" but not in the input.
--- I've tried a simple example in CodePen using Directives and an controller function, it worked...
Could it be a problem in the project's structure? If so, where should i look for the problem?
Upvotes: 0
Views: 1006
Reputation: 8652
Objects are passed by reference while primitive types (string, number and boolean) are passed by value. The variable addBrandInput is a string, you should change it to a reference type, otherwise two way binding won't work
$scope.addTheBrand = function(data){
$scope.addBrandInput = {
label: 'SomeText'
}
console.log("$scope.addBrandInput",$scope.addBrandInput);
};
Upvotes: 2