简体   繁体   English

AngularJS指令和控制器之间的两种方式绑定

[英]Angularjs two way binding between directive and controller

I got list of countries, which will be populated in a dropdown box. 我得到了国家列表,将在下拉框中填充该列表。 I made it as a directive with list items. 我将其作为带有列表项的指令。

And it on click of the country, the country name should be updated in $scope.selected which is defined in the controller. 单击国家/地区后,应在控制器中定义的$ scope.selected中更新国家/地区名称。

Unfortunately i couldnt pass the data from the directive to the controller if the list item is clicked. 不幸的是,如果单击列表项,则无法将数据从指令传递给控制器​​。 How should i map it so that on click it updates the name and code. 我应该如何映射它,以便在单击时更新名称和代码。

Here is the JSFIDDLE 这是JSFIDDLE

Thanks 谢谢

** Pasting the directive code ** **粘贴指令代码**

myApp.directive("dropSelect",function(){

    return{
            restrict:'E',
            scope : {
                items : '=items',
                selected:'=ngModel'
            },
            template:'<input type="text" ng-model="selected.name" placeholder="country">'+'<ul><li ng-repeat="item in items" ng-click="selectedCountry(item)">{{item.name}}<li></ul>',
           link : function(){

           },
            controller:function($scope){
            $scope.selectedCountry = function (item){
                console.log(item);
                $scope.selected.name = item.name
            }

            }        
    }

})

Edit 1: 编辑1:

adding @ symbol doesnt throw error but adding = symbol throws error 添加@符号不会引发错误,但是添加=符号则会引发错误

selected:'=ngModel' //throws error
selected:'@ngModel' //no error

I believe the issue is your selected object in your directive. 我相信问题是您在指令中selected对象。 The exception it throws leads towards that away. 它引发的异常导致了这一点。

In the html you bind with selected.name which means selected in your directive will be bound with the name property of selected in your controller. 在HTML你绑定selected.name这意味着selected在你的指令将由绑定name的属性selected您的控制器。

However when you select an item your trying to set a name property of the selected object in your directive which obviously it doesn't have as its just a string. 但是,当您选择一个项目时,您尝试在指令中设置selected对象的name属性,而该对象显然没有字符串。

So the solution: 所以解决方案:

       $scope.selectedCountry = function (item){
            console.log(item);
            $scope.selected = item.name
        }

Just set the name to selected in your directive. 只需在指令中将名称设置为selected

http://jsfiddle.net/abarfhr8/1/ http://jsfiddle.net/abarfhr8/1/


EDIT: 编辑:

However i would restructure it slightly. 但是我会稍微调整一下。 First initialize your selected object, so $scope.selected = $scope.items[0] in your controller. 首先初始化您选择的对象,所以$scope.selected = $scope.items[0]在您的控制器中。 Then change the ng-model to ng-model="selected" so that your returning the whole selected object. 然后将ng-model更改为ng-model="selected"以便您返回整个选定的对象。

finally in your directive do: 最后在您的指令中执行:

        $scope.selectedCountry = function (item){
            console.log(item);
            $scope.selected = item
        }

Now when you run and select an item the code and name inputs populate correctly. 现在,当您运行并选择一个项目时,代码和名称输入将正确填充。

See second fiddle: http://jsfiddle.net/abarfhr8/2/ 参见第二小提琴: http : //jsfiddle.net/abarfhr8/2/

Hope that helps. 希望能有所帮助。

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

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