简体   繁体   English

如何将变量从控制器传递到指令?

[英]How pass variables to directive from controller?

HTML: HTML:

<div ng-repeat="item in productArr">
   {{ item.title }}
</div>
<div category-page-navigation current-page='currentPage' category-products-count='productsCount'></div>

JS: JS:

.controller('categoryController', ['$scope', '$location', '$http', '$q', '$window', '$stateParams', function($scope, $location, $http, $q, $window, $stateParams) {

        $scope.currentPage = 1;
        $scope.productsCount = 0;               

        var GET = {
            getProductData: function() {
                var defer = $q.defer();

                $http.post('services/loadProduct.php', {
                    'id'    :1,
                }).then(function(response) {
                    defer.resolve(response);
                }, function(response) {             
                    defer.resolve([]);
                });         

                return defer.promise;               
            }
        };

        var getData = {
            getProduct: function() {
                var productData = GET.getProductData();

                $q.all([productData]).then(
                    function(response) {
                        $scope.productArr = response[0].data.products;
                        $scope.productsCount = response[0].data.products.length;
                    });         
            }
        };

        getData.getProduct(); 

    }])
    .directive('categoryPageNavigation', function($compile, $parse) {
        return {
            scope: {
                currentPage: '=currentPage',
                categoryProductsCount: '=categoryProductsCount'
            },
            link: function (scope, element, attrs) {
                debugger;
                // Here scope.categoryProductsCount = undefined
                // ...
                $scope.$watch(scope.currentPage, function(value) {
                    // ...
                });                             
            }
        };
    });

I try to form new HTML for navigation to manipulate with HTML I get from ng-repeat. 我尝试形成新的HTML进行导航,以处理从ng-repeat获得的HTML。 In directive I need currentPage(from start =1) and total count of items from ng-repeat(length of array) witch I get from service. 在指令中,我需要currentPage(从start = 1开始)和从服务获得的ng-repeat(数组的长度)女巫的项目总数。 How I can pass variables to directive? 如何将变量传递给指令? First I need to get variables from service(ajax request or something else) then pass variables(some ather data) to directive. 首先,我需要从服务(ajax请求或其他)获取变量,然后将变量(某些ather数据)传递给指令。

If I understood correctly what you mean. 如果我正确理解您的意思。 Here is a code pen example on how to shared data between you controller and your directive. 这是一个有关如何在控制器和指令之间共享数据的代码笔示例。

A good read to understand the code below: https://docs.angularjs.org/guide/providers 读懂以下代码的好书: https : //docs.angularjs.org/guide/providers

http://codepen.io/chocobowings/full/Xmzxmo/ http://codepen.io/chocobowings/full/Xmzxmo/

 var app = angular.module('app', []); //-------------------------------------------------------// app.factory('Shared', function() { return { sharedValue: { value: '', } }; }); //-------------------------------------------------------// app.controller('ctrl', function($scope, Shared) { $scope.model = Shared.sharedValue; }); //-------------------------------------------------------// app.directive('directive', ['Shared', function(Shared) { return { restrict: 'E', link: function(scope) { scope.model = Shared.sharedValue; }, template: '<div><input type="text" ng-model="model.value"/></div>' } } ]); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app"> Ctrl: <div ng-controller="ctrl"> <input type="text" ng-model="model.value" /> <br/> </div> Directive: <directive value="model.value"></directive> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM