8
In Angularjs, I was creating a directive.
I realized that by using the sign =
to capture a certain controller scope value, when I changed this value, the parent controller value was also being changed.
For example:
angular.module('app', [])
.controller('Parent', function ($scope) {
$scope.users = [{name: "Jonh"}, {name: "Wallace"}];
$scope.user = $scope.nomes[0];
})
.directive('userMoreInfo', function () {
var directive = {
replace: true,
restrict: 'E',
template: '<button ng-click="showInfo(user)">info</button>',
scope: {
os: '='
}
};
directive.controller = function ($scope) {
$scope.showInfo = function (user, modal) {
$http.get('/mais-informacoes/' + user.id).then(function (response) {
// isso aqui tá alterando o valor do controller pai
$scope.user = response.data;
// restante do código (não relevante à pergunta)
})
};
};
})
Calling for:
<div ng-controller="Parent">
<ul>
<li ng-repeat="user in users">
{{ user.name }}
<user-more-info></user-more-info>
</li>
</ul>
</div>
But I didn’t want this to happen. I wanted to use the Scope property with the same name, without affecting the parent controller.
I ended up using the <
and worked as I wanted.
Superficially, I understood that =
a value so that the "two sides" are affected. And <
assigns the value so that it is isolated in the scope
of the directive.
Therefore, I would like to know in detail what are the main differences between these two operators regarding the way in which the scope
of a particular Directive.
This explanation already exists for the other, see the example http://stackoverflow.com/questions/14050195/what-is-the-difference-between-and-in-directive-scope-in-angularjs
– KingRider