从子级更新父范围值

时间:2015-08-24 16:17:01

标签: angularjs angularjs-scope

我的代码如下

<!DOCTYPE html>
<html>
<head>

    <!-- CSS (load bootstrap) -->    <!-- JS (load angular, ui-router, and our custom js file) -->
    <script src="http://code.angularjs.org/1.2.13/angular.js"></script>
    <script src="app1.js"></script>
</head>

<!-- apply our angular app to our site -->
<body ng-app ='directivesModule'>
    <div class='main' ng-controller="customersController">
        <input type='text' ng-model='firsttext'>
        <button name='hello1' ng-click="showsecond = !showsecond;thirdtext='pradeep'">show second</button>
        <div class='second' ng-show='showsecond'>
            <input type='text'  ng-model='secondtext'>
            <button name='hello2' ng-click="showthird = !showthird">show third</button>
            {{firsttext}}
        </div>
         <div class='third' ng-if='showthird'>
            <input type='text'  ng-model='thirdtext'>
            <button name='hello' ng-click="changeparent()">hello</button>
            {{secondtext}}
            {{firsttext}}
        </div>
    </div>
</body>
</html>

JS看起来像

var app = angular.module('directivesModule', []);

    app.controller('customersController', ['$scope', function ($scope) {

        $scope.changeparent = function()
        {
            $scope.firsttext = $scope.thirdtext;
            console.log($scope.thirdtext);
        };

    }]);

可能代码很清楚我想要做什么。那么孩子的价值是什么,我试图将其更新为第一个文本框。但不可能。我知道我可以通过使用发射或传递参数来实现它。 BUt还有其他最好的方法来处理它。 Ng-show我不想用作元素将出现在DOM中。

0 个答案:

没有答案
相关问题