父控制器的调用方法从AngularJS中的指令
发布时间:2020-12-17 09:04:50 所属栏目:安全 来源:网络整理
导读:在我以前的 question,我现在试图从我的指令调用父控制器上的一个方法。我得到一个undefined参数。这里是我做的: body ng-app="myApp" ng-controller="MainCtrl" span{{mandat.rum}}/span span{{mandat.surname}}/spaninput type="text" ng-model="mandat.p
在我以前的
question,我现在试图从我的指令调用父控制器上的一个方法。我得到一个undefined参数。这里是我做的:
<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> 脚本: 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是undefined。 jsfiddle这里:http://jsfiddle.net/graphicsxp/Z5MBf/7/
只是简单的改变你的html如下
<my-directive mandate-person="mandat.person" updateparent="updatePerson(person)" > </my-directive> 你不是传递“人”与updatePerson这就是为什么它不工作 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |