相关疑难解决方法(0)

从AngularJS中的指令调用父控制器的方法

按照我之前的问题,我现在尝试从我的指令调用父控制器上的方法.我得到一个未定义的参数.这是我做的:

<body ng-app="myApp" ng-controller="MainCtrl">
  <span>{{mandat.rum}}</span>
  <span>{{mandat.surname}}</span>
<input type="text" ng-model="mandat.person.firstname" />
<my-directive mandate-person="mandat.person" updateparent="updatePerson()" >

  </my-directive>
</body>
Run Code Online (Sandbox Code Playgroud)

和剧本:

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

    app.controller('MainCtrl', function ($scope) {
        $scope.mandat = { name: "John", surname: "Doe", person: { id: 1408, firstname: "sam" } };
        $scope.updatePerson = function(person) {
            alert(person.firstname);
          $scope.mandat.person = person;   
        }
    });


    app.directive('myDirective', function () {
        return {
            restrict: 'E',
            template: "<div><span>{{mandatePerson.id}}<span><input type='text' ng-model='mandatePerson.firstname' /><button ng-click='updateparent({person: mandatePerson})'>click</button></div>",
            replace: true,
            scope: { mandatePerson: '=', updateparent: '&' }
            }
        }
    ) …
Run Code Online (Sandbox Code Playgroud)

angularjs angularjs-directive

69
推荐指数
3
解决办法
9万
查看次数

标签 统计

angularjs ×1

angularjs-directive ×1