AngularJS isolated directive watch for changes for all of the two way data bindings











up vote
1
down vote

favorite












In AngularJS, when something change in parent, the child binds the new change,
automatically. However this awesome process is being done only in the HTML file,
but not in the JS file.



Sometimes, if not always, we would like to acheive the same thing also in JS files.



Directive is getting initialized via the JS file, like this:



// statusBadge.js directive
app.directive("statusBadge", ($rootScope, ArrayManipulation, Http, Cast, Modules, Toast) => {
return {
templateUrl: "/shared/status-badge/status-badge.html",
scope: {
bindModel: "=ngModel",
statusChangingRequiredModules: "=",
statusOptionsToBeHide: "<",
onChange: "=",
resource: "@"
},
link: function($scope, element, attrs) {

if ($scope.bindModel==$rootScope._id) {
$scope.active = true;
}

}
}
});

// statusBadge.html directive
<span ng-show="active">
Active
</span>

// parent.js
$scope._id = "123";

// app.js
$rootScope._id = "123";

// parent.html
<status-badge ng-model="_id"></status-badge>


Now when data is synchronomous the "Active" will be displayed, since its getting
provided by initialization.



However, when working async - like receiving the data from the remote server,
I have to include ng if, like that:



// app.js 
setTimeout(function () {
$scope._id = "123";
}, 10);

// parent.html
// notice the ng-if
<status-badge ng-model="_id" ng-if="_id"></status-badge>


If I dont provide with the above ng-if, the directive configuation will be inited
without the actual data.



Front end, later, will have the data, since as I mentined in the begining - AngularJS
binds two way data, but only to HTML files.



Now I know I can put a watcher in the JS file, like this :



// statusBadge.js directive
app.directive("statusBadge", ($rootScope, ArrayManipulation, Http, Cast, Modules, Toast) => {
return {
templateUrl: "/shared/status-badge/status-badge.html",
scope: {
bindModel: "=ngModel",
statusChangingRequiredModules: "=",
statusOptionsToBeHide: "<",
onChange: "=",
resource: "@"
},
link: function($scope, element, attrs) {

$scope.$watch("bindModel", function() {
if ($scope.bindModel==$rootScope._id) {
$scope.active = true;
}
}, true);

}
}
});


However this can be tidy to so everytime, and sometimes the list of the watchers is long:



// almost all of them are two way data binding, but resource and statusOptionsToBeHide
scope: {
bindModel: "=ngModel",
statusChangingRequiredModules: "=",
statusOptionsToBeHide: "<",
onChange: "=",
resource: "@"
}


So what is the solution for this, or the best practice or just the preferred way of working?



Should I add the ng-if in each time its changed, and every time make it false - which can cause a bad UI - since the whole list, or component is going to be refreshed on each change - looks awful.



Should I put tons of watchers?



OR that is there any good solution for this?



Note that I have seen this question in Stack:
$watch'ing for data changes in an Angular directive



However they tell there that it should be a watcher in there, but I need a "multiple" watcher.










share|improve this question
























  • angularjs component has $onChanges method :huh:
    – Petr Averyanov
    Nov 14 at 14:19










  • And directives not? :)
    – Raz
    Nov 14 at 14:35










  • do not know :P -- after components were introduced, there is no sense to have directives with isolated scope
    – Petr Averyanov
    Nov 14 at 15:42










  • Can you post an answer and code please?
    – Raz
    Nov 14 at 18:39















up vote
1
down vote

favorite












In AngularJS, when something change in parent, the child binds the new change,
automatically. However this awesome process is being done only in the HTML file,
but not in the JS file.



Sometimes, if not always, we would like to acheive the same thing also in JS files.



Directive is getting initialized via the JS file, like this:



// statusBadge.js directive
app.directive("statusBadge", ($rootScope, ArrayManipulation, Http, Cast, Modules, Toast) => {
return {
templateUrl: "/shared/status-badge/status-badge.html",
scope: {
bindModel: "=ngModel",
statusChangingRequiredModules: "=",
statusOptionsToBeHide: "<",
onChange: "=",
resource: "@"
},
link: function($scope, element, attrs) {

if ($scope.bindModel==$rootScope._id) {
$scope.active = true;
}

}
}
});

// statusBadge.html directive
<span ng-show="active">
Active
</span>

// parent.js
$scope._id = "123";

// app.js
$rootScope._id = "123";

// parent.html
<status-badge ng-model="_id"></status-badge>


Now when data is synchronomous the "Active" will be displayed, since its getting
provided by initialization.



However, when working async - like receiving the data from the remote server,
I have to include ng if, like that:



// app.js 
setTimeout(function () {
$scope._id = "123";
}, 10);

// parent.html
// notice the ng-if
<status-badge ng-model="_id" ng-if="_id"></status-badge>


If I dont provide with the above ng-if, the directive configuation will be inited
without the actual data.



Front end, later, will have the data, since as I mentined in the begining - AngularJS
binds two way data, but only to HTML files.



Now I know I can put a watcher in the JS file, like this :



// statusBadge.js directive
app.directive("statusBadge", ($rootScope, ArrayManipulation, Http, Cast, Modules, Toast) => {
return {
templateUrl: "/shared/status-badge/status-badge.html",
scope: {
bindModel: "=ngModel",
statusChangingRequiredModules: "=",
statusOptionsToBeHide: "<",
onChange: "=",
resource: "@"
},
link: function($scope, element, attrs) {

$scope.$watch("bindModel", function() {
if ($scope.bindModel==$rootScope._id) {
$scope.active = true;
}
}, true);

}
}
});


However this can be tidy to so everytime, and sometimes the list of the watchers is long:



// almost all of them are two way data binding, but resource and statusOptionsToBeHide
scope: {
bindModel: "=ngModel",
statusChangingRequiredModules: "=",
statusOptionsToBeHide: "<",
onChange: "=",
resource: "@"
}


So what is the solution for this, or the best practice or just the preferred way of working?



Should I add the ng-if in each time its changed, and every time make it false - which can cause a bad UI - since the whole list, or component is going to be refreshed on each change - looks awful.



Should I put tons of watchers?



OR that is there any good solution for this?



Note that I have seen this question in Stack:
$watch'ing for data changes in an Angular directive



However they tell there that it should be a watcher in there, but I need a "multiple" watcher.










share|improve this question
























  • angularjs component has $onChanges method :huh:
    – Petr Averyanov
    Nov 14 at 14:19










  • And directives not? :)
    – Raz
    Nov 14 at 14:35










  • do not know :P -- after components were introduced, there is no sense to have directives with isolated scope
    – Petr Averyanov
    Nov 14 at 15:42










  • Can you post an answer and code please?
    – Raz
    Nov 14 at 18:39













up vote
1
down vote

favorite









up vote
1
down vote

favorite











In AngularJS, when something change in parent, the child binds the new change,
automatically. However this awesome process is being done only in the HTML file,
but not in the JS file.



Sometimes, if not always, we would like to acheive the same thing also in JS files.



Directive is getting initialized via the JS file, like this:



// statusBadge.js directive
app.directive("statusBadge", ($rootScope, ArrayManipulation, Http, Cast, Modules, Toast) => {
return {
templateUrl: "/shared/status-badge/status-badge.html",
scope: {
bindModel: "=ngModel",
statusChangingRequiredModules: "=",
statusOptionsToBeHide: "<",
onChange: "=",
resource: "@"
},
link: function($scope, element, attrs) {

if ($scope.bindModel==$rootScope._id) {
$scope.active = true;
}

}
}
});

// statusBadge.html directive
<span ng-show="active">
Active
</span>

// parent.js
$scope._id = "123";

// app.js
$rootScope._id = "123";

// parent.html
<status-badge ng-model="_id"></status-badge>


Now when data is synchronomous the "Active" will be displayed, since its getting
provided by initialization.



However, when working async - like receiving the data from the remote server,
I have to include ng if, like that:



// app.js 
setTimeout(function () {
$scope._id = "123";
}, 10);

// parent.html
// notice the ng-if
<status-badge ng-model="_id" ng-if="_id"></status-badge>


If I dont provide with the above ng-if, the directive configuation will be inited
without the actual data.



Front end, later, will have the data, since as I mentined in the begining - AngularJS
binds two way data, but only to HTML files.



Now I know I can put a watcher in the JS file, like this :



// statusBadge.js directive
app.directive("statusBadge", ($rootScope, ArrayManipulation, Http, Cast, Modules, Toast) => {
return {
templateUrl: "/shared/status-badge/status-badge.html",
scope: {
bindModel: "=ngModel",
statusChangingRequiredModules: "=",
statusOptionsToBeHide: "<",
onChange: "=",
resource: "@"
},
link: function($scope, element, attrs) {

$scope.$watch("bindModel", function() {
if ($scope.bindModel==$rootScope._id) {
$scope.active = true;
}
}, true);

}
}
});


However this can be tidy to so everytime, and sometimes the list of the watchers is long:



// almost all of them are two way data binding, but resource and statusOptionsToBeHide
scope: {
bindModel: "=ngModel",
statusChangingRequiredModules: "=",
statusOptionsToBeHide: "<",
onChange: "=",
resource: "@"
}


So what is the solution for this, or the best practice or just the preferred way of working?



Should I add the ng-if in each time its changed, and every time make it false - which can cause a bad UI - since the whole list, or component is going to be refreshed on each change - looks awful.



Should I put tons of watchers?



OR that is there any good solution for this?



Note that I have seen this question in Stack:
$watch'ing for data changes in an Angular directive



However they tell there that it should be a watcher in there, but I need a "multiple" watcher.










share|improve this question















In AngularJS, when something change in parent, the child binds the new change,
automatically. However this awesome process is being done only in the HTML file,
but not in the JS file.



Sometimes, if not always, we would like to acheive the same thing also in JS files.



Directive is getting initialized via the JS file, like this:



// statusBadge.js directive
app.directive("statusBadge", ($rootScope, ArrayManipulation, Http, Cast, Modules, Toast) => {
return {
templateUrl: "/shared/status-badge/status-badge.html",
scope: {
bindModel: "=ngModel",
statusChangingRequiredModules: "=",
statusOptionsToBeHide: "<",
onChange: "=",
resource: "@"
},
link: function($scope, element, attrs) {

if ($scope.bindModel==$rootScope._id) {
$scope.active = true;
}

}
}
});

// statusBadge.html directive
<span ng-show="active">
Active
</span>

// parent.js
$scope._id = "123";

// app.js
$rootScope._id = "123";

// parent.html
<status-badge ng-model="_id"></status-badge>


Now when data is synchronomous the "Active" will be displayed, since its getting
provided by initialization.



However, when working async - like receiving the data from the remote server,
I have to include ng if, like that:



// app.js 
setTimeout(function () {
$scope._id = "123";
}, 10);

// parent.html
// notice the ng-if
<status-badge ng-model="_id" ng-if="_id"></status-badge>


If I dont provide with the above ng-if, the directive configuation will be inited
without the actual data.



Front end, later, will have the data, since as I mentined in the begining - AngularJS
binds two way data, but only to HTML files.



Now I know I can put a watcher in the JS file, like this :



// statusBadge.js directive
app.directive("statusBadge", ($rootScope, ArrayManipulation, Http, Cast, Modules, Toast) => {
return {
templateUrl: "/shared/status-badge/status-badge.html",
scope: {
bindModel: "=ngModel",
statusChangingRequiredModules: "=",
statusOptionsToBeHide: "<",
onChange: "=",
resource: "@"
},
link: function($scope, element, attrs) {

$scope.$watch("bindModel", function() {
if ($scope.bindModel==$rootScope._id) {
$scope.active = true;
}
}, true);

}
}
});


However this can be tidy to so everytime, and sometimes the list of the watchers is long:



// almost all of them are two way data binding, but resource and statusOptionsToBeHide
scope: {
bindModel: "=ngModel",
statusChangingRequiredModules: "=",
statusOptionsToBeHide: "<",
onChange: "=",
resource: "@"
}


So what is the solution for this, or the best practice or just the preferred way of working?



Should I add the ng-if in each time its changed, and every time make it false - which can cause a bad UI - since the whole list, or component is going to be refreshed on each change - looks awful.



Should I put tons of watchers?



OR that is there any good solution for this?



Note that I have seen this question in Stack:
$watch'ing for data changes in an Angular directive



However they tell there that it should be a watcher in there, but I need a "multiple" watcher.







javascript angularjs asynchronous listener directive






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 at 18:18









marc_s

567k12810981249




567k12810981249










asked Nov 14 at 8:18









Raz

20410




20410












  • angularjs component has $onChanges method :huh:
    – Petr Averyanov
    Nov 14 at 14:19










  • And directives not? :)
    – Raz
    Nov 14 at 14:35










  • do not know :P -- after components were introduced, there is no sense to have directives with isolated scope
    – Petr Averyanov
    Nov 14 at 15:42










  • Can you post an answer and code please?
    – Raz
    Nov 14 at 18:39


















  • angularjs component has $onChanges method :huh:
    – Petr Averyanov
    Nov 14 at 14:19










  • And directives not? :)
    – Raz
    Nov 14 at 14:35










  • do not know :P -- after components were introduced, there is no sense to have directives with isolated scope
    – Petr Averyanov
    Nov 14 at 15:42










  • Can you post an answer and code please?
    – Raz
    Nov 14 at 18:39
















angularjs component has $onChanges method :huh:
– Petr Averyanov
Nov 14 at 14:19




angularjs component has $onChanges method :huh:
– Petr Averyanov
Nov 14 at 14:19












And directives not? :)
– Raz
Nov 14 at 14:35




And directives not? :)
– Raz
Nov 14 at 14:35












do not know :P -- after components were introduced, there is no sense to have directives with isolated scope
– Petr Averyanov
Nov 14 at 15:42




do not know :P -- after components were introduced, there is no sense to have directives with isolated scope
– Petr Averyanov
Nov 14 at 15:42












Can you post an answer and code please?
– Raz
Nov 14 at 18:39




Can you post an answer and code please?
– Raz
Nov 14 at 18:39

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295696%2fangularjs-isolated-directive-watch-for-changes-for-all-of-the-two-way-data-bindi%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295696%2fangularjs-isolated-directive-watch-for-changes-for-all-of-the-two-way-data-bindi%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents