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.
javascript angularjs asynchronous listener directive
add a comment |
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.
javascript angularjs asynchronous listener directive
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
add a comment |
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.
javascript angularjs asynchronous listener directive
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
javascript angularjs asynchronous listener directive
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
add a comment |
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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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