angular.js - How to get values ​​from two direcitve
曾经蜡笔没有小新
曾经蜡笔没有小新 2017-05-15 16:54:59
0
1
596


How to get the values ​​​​in the corresponding scopes of two different DIRECTIVEs
JS code

.directive('save',function(){
    return{
        restrict:'EAC',
        template:'<p class="saveBtn bottomBtn fl" id="btnSave"><img src="images/savePic.png"></p>',
        scope:{ goodsName: '@goodName'},
        link:function(scope,element,attrs){
            var childElem = element.find('toggleName');
            var childScope = childElem.isolateScope();
            element.on('click', function() {
                    var jsonData = scope.goodName;
                    alert(jsonData);            
            });
        }
    };
});
 .directive('toggleName', function() {
        return {
            restrict: 'ECA',
            templateUrl: 'views/partials/toggleName.html',
            transclute: true,
            link: function(scope, element, attrs) {
                scope.toggleName = function() {
                    scope.isSuccessName = !scope.isSuccessName;
                };
            }
        };
    })

HTML code

 <p class="realInputCon fr">
        <input type="text" maxlength="255" placeholder="请输入" class="realCodeField realFieldCommon" ng-model="goodName">
    </p>

save gets the goodName in togglename

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

reply all(1)
某草草

Just use require


 .directive('toggleName', function() {
        return {
            restrict: 'ECA',
            templateUrl: 'views/partials/toggleName.html',
            controller: function($scope) {
                $scope.goodName = 'togglename';
                this.getName = function() {
                    return $scope.goodName;
                };
            },
            transclute: true,
            link: function(scope, element, attrs) {
                scope.toggleName = function() {
                    scope.isSuccessName = !scope.isSuccessName;
                };
            }
        };
    })

.directive('save',function(){
    return{
        restrict:'EAC',
        template:'<p class="saveBtn bottomBtn fl" id="btnSave"><img src="images/savePic.png"></p>',
        require: 'toggleNmae',
        link:function(scope,element,attrs, toggleNameController){
            var childElem = element.find('toggleName');
            var childScope = childElem.isolateScope();
            element.on('click', function() {
                    var jsonData = scope.goodName;
                    alert(jsonData);            
            });
            //这里就是toggleName控制器的使用了
            toggleNameController.getName();
        }
    };
});
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template