AngularJS:从指令传递值控制器 [英] AngularJS : Passing value from directive to controller

查看:131
本文介绍了AngularJS:从指令传递值控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉复制的问题。但我发现一些有用的东西和更新它。

Sorry for duplicating the question. But I found something useful and updated with it.

这是我的指令:

App.directive('mapUsa', [function() {

return {

    restrict: 'A',
    link: function(scope, element, attrs) {

        element.vectorMap({
            map: 'us_aea_en',
            backgroundColor: 'transparent',
            zoomButtons: false,
            regionStyle: {
                initial: {
                    fill: '#343434' 
                },
                hover: {
                    fill: '#242424'
                }
            },
            markerStyle: {
                initial: {
                    fill: '#F0A518',
                    stroke: '#F0A518',
                }
            },
            onRegionClick: function(event, code) {

            },
            markers: [
                {latLng: [37.78, -122.41], name: 'San Francisco', style: {r: 10}},
                {latLng: [40.71, -74], name: 'New York City', style: {r: 15}},
                {latLng: [41.89, -87.62], name: 'Chicago', style: {r: 5}},
                {latLng: [34.00, -118.25], name: 'Los Angeles', style: {r: 20}},
                {latLng: [34.00, -106.00], name: 'New Mexico', style: {r: 10}},
                {latLng: [44.50, -100.00], name: 'South Dakota', style: {r: 13}},
                {latLng: [25.78, -80.22], name: 'Miami', style: {r: 7}},
            ]
        });

    },
    controller: ['$scope', 'citiesData', function($scope, citiesData) {

        citiesData.getCitiesData()

        .then(function(data) {
            $scope.cities = data;

        }, function(errorStatus) {
            $scope.errorStatus = errorStatus;
        })



    }]

}

}])

在我的控制器我加载一个JSON文件,我可以在用角前pression我的HTML的课程访问的。

In my controller I'm loading a json file and I can of course access it with my HTML using the angular expression.

但现在的 onRegionClick 我得到城市的code每当我点击的区域。

But now on the onRegionClick I get the code of the cities whenever I click on a region.

这是JSON的样子。

{ "US-AL": { "name": "Alabama", "population": 4833722 }, "US-AK": { "name": "Alaska", "population": 735132 }

我创建JSON与code对象,我从获得 onRegionClick

我想最终使用code从城市code我得到 onRegionClick 并传递给它的控制器,然后打印出的人口数从JSON获取数据。

I wanted to print out the population count from the city code I get on onRegionClick and passing to it the controller and then finally using that code to get the data from JSON.

请不要帮我在这。由于我也没当回事未能就如何从指令传递参数值到控制器理解。

Please do help me out in this. As I didn't seriously couldn't understand on how to pass a param value from directive to a controller.

在我的HTML我用前pression这样从控制器创建的变量。

In my HTML I used expression like this from the variable created in the controller.

{{ cities.AL.name }}

这是OFC工作的罚款。但我想,以取代 AL 与code,我从获得 onRegionClick

It is ofc working fine. But I wanted to replace the AL with the code I get from the onRegionClick.

先谢谢了。

推荐答案

如何使用过滤器?也许你可以使用默认的角度过滤器和显示过滤后的数据。

How about using a filter? Maybe you can use the angular default filter and display the filtered data.

JSON:

{
   {
      "code": "US-AL",
      "name": "Alabama",
      "population": 4833722
   }, {
      "code": "US-AK",
      "name": "Alaska",
      "population": 735132
   }
}

HTML

<tr ng-repeat="city in cities | filter:inputCode">
   <td>{{city.name}}</td>
</tr>

这篇关于AngularJS:从指令传递值控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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