Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了angularjs – Angular指令:绑定到父作用域中的变量大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
角度指令演示:

jsfiddle

<div ng-app="myApp">
<script>
    function Contrl($scopE){
        $scope.parval = 0;
        $scope.items = [
            {id: 1,text: '1'},{id: 2,text: '2'},{id: 3,text: '3'}
        ];
    }
</script>
<div ng-controller="Contrl">
    A: <input type="radio" value="1" ng-model="parval">1</input>
    <input type="radio" value="2" ng-model="parval">2</input>
    <input type="radio" value="3" ng-model="parval">3</input>
    <item parval="parval" items="items"></item>
</div>
angular.module('myApp',[])
.directive('item',function() {
    return {
        reStrict: 'E',replace: true,scope: {
            parval: '=',items: '='
        },template: '<div>' +
        'B: <span ng-repeat="i in items">' +
                '<input value="{{i.iD}}" type="radio" ng-model="parval">{{i.text}}</input>&nbsp;' +
            '</span>' +
        '</div>'
    };
});

现在:
点击A1 – > B1选中
点击A2 – > B2选中

点击B1 – > A1没变
点击B2 – > A2没变

我想要:
点击A1 – > B1选中
点击A2 – > B2选中

点击B1 – > A1选中了
点击B2 – > A2选中

怎么样?

你正在使用原语,你应该避免使用文字表示法,因为ng-repeat创建了一个新的范围.以下代码解决您的问题
<div ng-controller="Contrl">
    A: <input type="radio" value="1" ng-model="parval.value">1</input>
    <input type="radio" value="2" ng-model="parval.value">2</input>
    <input type="radio" value="3" ng-model="parval.value">3</input>
    <item parval="parval" items="items"></item>
</div>
    <script>
        function Contrl($scopE) {
            $scope.parval = { value: 0 };
            $scope.items = [
                { id: 1,text: '1' },{ id: 2,text: '2' },{ id: 3,text: '3' }
            ];
        }
        angular.module('myApp',function () {
    return {
        reStrict: 'E',template: '<div>' +
        'B: <span ng-repeat="i in items">' +
                '<input value="{{i.iD}}" type="radio" ng-model="parval.value">{{i.text}}</input>&nbsp;' +
            '</span>' +
        '</div>'
    };
});
    </script>

大佬总结

以上是大佬教程为你收集整理的angularjs – Angular指令:绑定到父作用域中的变量全部内容,希望文章能够帮你解决angularjs – Angular指令:绑定到父作用域中的变量所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。