angular.js - Can directives in Angular accept assignments of type object?
曾经蜡笔没有小新
曾经蜡笔没有小新 2017-05-15 16:52:52
0
3
683

I want to write a list
Use directive to write an item
This item will change accordingly according to the different values ​​assigned, but currently I only know simple data assignment
For example

.directive('al-item', function() {
    return {
        restrict: 'EA',
        template: '

<p>'
                 +'</p>

',
        replace: true,
        controller: function($scope, $element, $attrs, $transclude) {
            //这里根据a、b、c 三者的值 进一步修饰al-item
        }
    };
})

<al-item a='' b='' c=''>

The form I want is more like

<al-item object=''>

Provide an object data like this
al-item can achieve the look I planned
Is this possible?

曾经蜡笔没有小新
曾经蜡笔没有小新

reply all(3)
阿神

You can look at the scope part of the instruction to solve your questions.

Approximately:

html<p ng-controller="cc">
<al-item object="obj"></al-item>
</p>
jsangular.module('xx', [])
.controller('cc', ['$scope', function($scope) {
    $scope.obj = {a:'a', b:'b', 'c':'c'}
}])
.directive('alItem', function() {
    return {
        restrict: 'EA',
        template: '<p>'+'</p>',
        replace: true,
        transclude: true,
        scope: {
            object: "="
        },
        controller: function($scope, $element, $attrs, $transclude) {
            //scope.object 这里就可以判断了
        }
    };
})

Probably like this. For details, you can read the official documentation

Ty80

Directive scope itself supports three modes
1. "=" any object
2. "&" The external method is passed into the directive and called internally
3. "@" string

我想大声告诉你

The object in HTML is also an attrs. Attrs has been transferred into the function. You can use attrs to call object directly.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template