Skip to main content
code indentation
Source Link
GillesC
  • 10.9k
  • 3
  • 42
  • 55

You can inject the service while defining the directive and then set up a $watch on the data. So in your case:

.directive('tree', function ($compile, myService) {
    return {
        // ...
        link: function (scope, element, attrs) {
            scope.$watch(function () {
                return myService.getData();
            },
            function (newVal) {
                if (typeof newVal !== 'undefined') {
                    // ...
                }
            }
        });
 
    }
}
});

This will watch the data for change and run the code each time the data changes.


However, if the data does not change after being set once, a better way (more efficient) would be to return a promise from the service ($http methods returns a promise directly or you can create one using the $q service) and then add your computation as a continuation to the data.

.directive('tree', function ($compile, myService) {
    return {
        // ...
        link: function (scope, element, attrs) {
            myService.getData().then(function (data) {
                // ...
            }, function (err) {
                // Handle error
            });
 
        }
    }
});

You can inject the service while defining the directive and then set up a $watch on the data. So in your case:

.directive('tree', function ($compile, myService) {
return {
    // ...
    link: function (scope, element, attrs) {
        scope.$watch(function () { return myService.getData(); },
            function (newVal) {
              if (typeof newVal !== 'undefined') {
                   // ...
              }
            }
        });
 
    }
}
});

This will watch the data for change and run the code each time the data changes.


However, if the data does not change after being set once, a better way (more efficient) would be to return a promise from the service ($http methods returns a promise directly or you can create one using the $q service) and then add your computation as a continuation to the data.

.directive('tree', function ($compile, myService) {
return {
    // ...
    link: function (scope, element, attrs) {
        myService.getData().then(function (data) {
            // ...
        }, function (err) {
            // Handle error
        });
 
    }
}
});

You can inject the service while defining the directive and then set up a $watch on the data. So in your case:

.directive('tree', function ($compile, myService) {
    return {
        // ...
        link: function (scope, element, attrs) {
            scope.$watch(function () {
                return myService.getData();
            },
            function (newVal) {
                if (typeof newVal !== 'undefined') {
                    // ...
                }
            }
        });
    }
});

This will watch the data for change and run the code each time the data changes.


However, if the data does not change after being set once, a better way (more efficient) would be to return a promise from the service ($http methods returns a promise directly or you can create one using the $q service) and then add your computation as a continuation to the data.

.directive('tree', function ($compile, myService) {
    return {
        // ...
        link: function (scope, element, attrs) {
            myService.getData().then(function (data) {
                // ...
            }, function (err) {
                // Handle error
            });
        }
    }
});
Typos
Source Link
musically_ut
  • 34.3k
  • 9
  • 98
  • 110

You can inject the service while defining the directive and then set up a $watch on the data. So in your case:

.directive('tree', function ($compile, myService) {
return {
    // ...
    link: function (scope, element, attrs) {
        scope.$watch(function () { return myService.getData(); },
            function (newVal) {
              if (typeof newVal !== 'undefined') {
                   // ...
              }
            }
        });

    }
}
});

This will watch the data for change and run the code each time the data changes.


However, if the data does not change after being set once, a better way (more efficient) would be to return a promise from the service (a $http methods returns a promise directly or you can create one using the $q serivceservice) and then add your computation as a continuation to the data.

.directive('tree', function ($compile, myService) {
return {
    // ...
    link: function (scope, element, attrs) {
        myService.getData().then(function (data) {
            // ...
        }, function (err) {
            // Handle error
        });

    }
}
});

You can inject the service while defining the directive and then set up a $watch on the data. So in your case:

.directive('tree', function ($compile, myService) {
return {
    // ...
    link: function (scope, element, attrs) {
        scope.$watch(function () { return myService.getData(); },
            function (newVal) {
              if (typeof newVal !== 'undefined') {
                   // ...
              }
            }
        });

    }
}
});

This will watch the data for change and run the code each time the data changes.


However, if the data does not change after being set once, a better way (more efficient) would be to return a promise from the service (a $http returns a promise directly or you can create one using the $q serivce) and then add your computation as a continuation to the data.

.directive('tree', function ($compile, myService) {
return {
    // ...
    link: function (scope, element, attrs) {
        myService.getData().then(function (data) {
            // ...
        }, function (err) {
            // Handle error
        });

    }
}
});

You can inject the service while defining the directive and then set up a $watch on the data. So in your case:

.directive('tree', function ($compile, myService) {
return {
    // ...
    link: function (scope, element, attrs) {
        scope.$watch(function () { return myService.getData(); },
            function (newVal) {
              if (typeof newVal !== 'undefined') {
                   // ...
              }
            }
        });

    }
}
});

This will watch the data for change and run the code each time the data changes.


However, if the data does not change after being set once, a better way (more efficient) would be to return a promise from the service ($http methods returns a promise directly or you can create one using the $q service) and then add your computation as a continuation to the data.

.directive('tree', function ($compile, myService) {
return {
    // ...
    link: function (scope, element, attrs) {
        myService.getData().then(function (data) {
            // ...
        }, function (err) {
            // Handle error
        });

    }
}
});
Better formatting.
Source Link
musically_ut
  • 34.3k
  • 9
  • 98
  • 110

You can inject the service while defining the directive and then set up a $watch on the data. So in your case:

.directive('tree', function ($compile, myService) {
return {
    // ...
    link: function (scope, element, attrs) {
        scope.$watch(function () { return myService.getData(); },
            function (newVal) {
              if (typeof newVal !== 'undefined') {
                   // ...
              }
            }
        });

    }
}
});

This will watch the data for change and run the code each time the data changes.


However, if the data does not change after fetchingbeing set once, a better way (more efficient) would be to return a promise from the service (a $http returns a promise directly or you can create one using the $q serivce) and then add your computation as a continuation to the data.

.directive('tree', function ($compile, myService) {
return {
    // ...
    link: function (scope, element, attrs) {
        myService.getData().then(function (data) {
            // ...
        }, function (err) {
            // Handle error
        });

    }
}
});

You can inject the service while defining the directive and then set up a $watch on the data. So in your case:

.directive('tree', function ($compile, myService) {
return {
    // ...
    link: function (scope, element, attrs) {
        scope.$watch(function () { return myService.getData(); },
            function (newVal) {
              if (typeof newVal !== 'undefined') {
                   // ...
              }
            }
        });

    }
}
});

However, if the data does not change after fetching, a better way would be to return a promise from the service (a $http returns a promise directly or you can create one using the $q serivce) and then add your computation as a continuation to the data.

.directive('tree', function ($compile, myService) {
return {
    // ...
    link: function (scope, element, attrs) {
        myService.getData().then(function (data) {
            // ...
        }, function (err) {
            // Handle error
        });

    }
}
});

You can inject the service while defining the directive and then set up a $watch on the data. So in your case:

.directive('tree', function ($compile, myService) {
return {
    // ...
    link: function (scope, element, attrs) {
        scope.$watch(function () { return myService.getData(); },
            function (newVal) {
              if (typeof newVal !== 'undefined') {
                   // ...
              }
            }
        });

    }
}
});

This will watch the data for change and run the code each time the data changes.


However, if the data does not change after being set once, a better way (more efficient) would be to return a promise from the service (a $http returns a promise directly or you can create one using the $q serivce) and then add your computation as a continuation to the data.

.directive('tree', function ($compile, myService) {
return {
    // ...
    link: function (scope, element, attrs) {
        myService.getData().then(function (data) {
            // ...
        }, function (err) {
            // Handle error
        });

    }
}
});
Source Link
musically_ut
  • 34.3k
  • 9
  • 98
  • 110
Loading