首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >AngularJS中从指令调用父控制器的方法

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

Stack Overflow用户
提问于 2013-04-14 02:08:07
回答 1查看 87.2K关注 0票数 69

在前面的question之后,我现在尝试从我的指令中调用父控制器上的一个方法。我得到一个未定义的参数。下面是我要做的:

代码语言:javascript
复制
<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>

和脚本:

代码语言:javascript
复制
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: '&' }
            }
        }
    )

当调用updatePerson方法时,person是未定义的。

jsfiddle这里:http://jsfiddle.net/graphicsxp/Z5MBf/7/

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-14 02:45:25

只需像下面这样简单地更改您的html

代码语言:javascript
复制
<my-directive mandate-person="mandat.person" updateparent="updatePerson(person)" >

      </my-directive>

您没有向updatePerson传递"person“,这就是它不起作用的原因

票数 57
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15991137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档