$scope.users
and display them in a view:
<user-list/>
ng-repeat
better than the user-list
because it is responsible for only one action: It only repeats a specific part, so it can be used in many situations. Easy to understand what she does. Instead of making one directive that is responsible for everything, it is better to break it into several directives that will perform specific tasks, and use them together.
ng-model
, is more useful than a directive that assumes that $scope.users
exists. As a rule, if your directive should be used in various applications, it should follow this rule so that you can say that it is well developed, even if you are not going to publish it.
photo-src
to specify the source of the image, and caption
for the signature. Be careful not to use names already used by other directives, such as ng-src
, if you do not know how they work.
photo
be an element.
<photo photo-src="{{photo.url}}" caption="Taken on: {{photo.date}}"/>
attrs.$observe
. In this case, the callback function will be called each time the value of the associated property is changed. Then we use element
to make changes to the DOM.
app.directive('photo', function() { return { // , restrict: 'E', // <photo> html template: '<figure><img/><figcaption/></figure>', replace: true, // DOM link: function($scope, element, attrs) { attrs.$observe('caption', function(value) { element.find('figcaption').text(value) }) // «» attrs.$observe('photoSrc', function(value) { element.find('img').attr('src', value) }) } } } })
app.directive('photo', function() { return { restrict: 'E', templateUrl: 'photo.html', replace: true, // attrs scope: { caption: '@', photoSrc: '@' } } })
<!-- photo.html --> <figure> <img ng-src="{{photoSrc}}"/> <figcaption>{{caption}}</figcaption> </figure>
ng-model
.
<!-- --> <button toggle="preferences.showDetails">Show Details</button>
=
in scope
: to make scope.toggle
available in our directive. Although it is not explicitly specified anywhere inside the directive, using this syntax, scope.toggle
reads and writes the property that the user specified in the attribute.
app.directive('toggle', function() { return { scope: { toggle: '=', }, link: function($scope, element, attrs) {
scope.$watch
, which performs the function passed to it every time the value of the expression changes. We will add or remove the active
class css, inside the handler called during changes.
$scope.$watch("toggle", function(value) { element.toggleClass('active', value) })
click
event, where we will update the scope. We need to use scope.$apply
every time changes occur outside of the Angular execution context.
element.click(function() { $scope.$apply(function() { $scope.toggle = !$scope.toggle }) }) } } })
ng-click
. Let's make a scroll
directive that can call a function when the user scrolls an element. In addition, let's also handle the scroll offset value.
...
scroll
attribute to the scope of our directive.
app.directive('scroll', function() { return { scope: { scroll: "&" }, link: function($scope, element, attrs) {
scope.$apply
, because, although the handler is called, it is not called in the context of the controller.
element.scroll(function() { $scope.apply(function() { var offset = element.scrollTop() $scope.scroll({offset:offset}) }) }) } } })
onScroll(offset)
expression that was passed through the attribute. This is a much more flexible approach than passing parameters directly, since other scope parameters can be passed to the corresponding functions, for example, the current element in ng-repeat
.
modal
component: a pop-up window with a close button for which you want to save its contents specified in html.
<modal> <p>Some contents</p> <p>Put whatever you want in here</p> </modal>
modal
element consists of more than one element. When we make a template, we insert all received content into it, where necessary, simply by adding a special ng-transclude
to the div
.
<div class="modal"> <header> <button>Close</button> <h2>Modal</h2> </header> <div class="body" ng-transclude></div> </div>
transclude: true
:
app.directive('modal', function() { return { restrict: 'E', templateUrl: 'modal.html', replace: true, transclude: true, } })
windowShown
variable in the controller, to which you bind using ng-show
, or transfer the logical value to your directive, in the manner described above.
$scope.$on
in a directive, but for a start, instead, try to think about the problem in terms of state changes. In Angular, things get much easier if you focus on data and state, instead of events.
Source: https://habr.com/ru/post/200620/