javascript - angular中的directive如何跟外部的controller 双向绑定

查看:108
本文介绍了javascript - angular中的directive如何跟外部的controller 双向绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

1.angular 中directive 和 外部controller 没法双向绑定

2.angular 代码

angular.module('ngFinder',[])
.controller('selectfile', function($scope){

})
.directive('elFinder', function() {
    return {
        restrict: 'E',
        scope: {
            'url': '=url',
            'selectFiles': '=',
        },
        link: function(scope, element, attrs, controller) {

            console.log("selectfiles", selectfiles)
            var options = {
                url : scope.url || '/elfinder',
                commands : false,
                handlers : {
                    select : function(event, elfinderInstance) {
                        var selectfile = $('.ui-selected .elfinder-cwd-filename').text();
                        scope.selectFiles = selectfile;
                    }
                }

            };
            $(element).elfinder(options);
        },
    };
})

3.html代码

<div id="fileSelectedContainer" ng-controller="FileController">
    <div class="row">
        <div class="col-md-6">
            <ul  style="list-style:none;line-height:24px;width:240px;margin-left:20px;background: #d4d4d4;">
                <li>{{selectefiles}}</li>
            </ul>
        </div>
        <div class="col-md-6" >
            <el-Finder class="checked" select-files="selectefile" data-url="'http://192.168.5.155:81/php/connector.minimal.php?rootPath=/'" ></el-Finder>
        </div>
    </div>

4.controller 代码

app.controller('FileController', function($scope, $rootScope, $http, $location, $window, $routeParams) {

        $scope.selectefiles = [];

        console.log("selectefiles", selectefiles);

});

5截图

{{selectfiles}}主要是这个没解析出来 我把 directive删了单独调,也没有解析

解决方案

scope.$apply(function(){
    scope.selectFiles = selectfile;
});


<li>{{selectFile}}</li>

这篇关于javascript - angular中的directive如何跟外部的controller 双向绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆