简体   繁体   English

将值从指令传递到控制器

[英]Passing values from directive to controller

Below is my html template: 以下是我的html模板:

<div ng-app="dr" ng-controller="testCtrl">
    <test color1="color1" data-method="ctrlFn(msg)"></test>    
</div>

Below is my code: 下面是我的代码:

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

app.controller("testCtrl", function($scope) {
    $scope.ctrlFn = function(arg) {        
        alert(arg);
    }

});
app.directive('test', function() {
    return {
        restrict: 'E',
        scope: {
            fromDirectiveFn: '&method'
        },
        link: function(scope, elm, attrs) {
            //Way One
            scope.hello = "some message";
            scope.fromDirectiveFn(scope.hello);
        }
    }
});

<div ng-app="dr" ng-controller="testCtrl">
    <test color1="color1" data-method="ctrlFn(msg)"></test>    
</div>

Why am i getting " undefined " instead of " some message " 为什么我收到“ 未定义 ”而不是“ 某些消息

Below is a fiddle http://jsfiddle.net/j2K7N/27/ 下面是一个小提琴http://jsfiddle.net/j2K7N/27/

Your code is almost correct, however you had several issues here: 您的代码几乎是正确的,但是这里有几个问题:

<test color1="color1" data-method="ctrlFn(msg)"></test>

Here you pass the ctrlFn() function from your controller, which takes one undefined argument msg , that causes the alert message with "undefined" text. 在这里,您从控制器传递了ctrlFn()函数,该函数采用一个未定义的参数msg ,该参数导致带有“未定义”文本的警报消息。 I suggest to modify the HTML code to this: 我建议将HTML代码修改为:

<test color1="color1" data-method="ctrlFn"></test>  

Note that I pass the ctrlFn as a variable, not function. 请注意,我将ctrlFn作为变量而不是函数传递。

In your directive code, I changed the scope binding to = to make sure that the ctrlFn will point to your controller function. 在您的指令代码中,我将范围绑定更改为= ,以确保ctrlFn指向您的控制器函数。 This also sets up a two-way binding between the directive's scope and the controller (parent) scope. 这还将在指令的作用域和控制器(父)作用域之间建立双向绑定。 Then the whole JS code of the directive will look like this: 然后,该指令的整个JS代码将如下所示:

app.directive('test', function() {
    return {
        restrict: 'E',
        scope: {
            fromDirectiveFn: '=method'
        },
        link: function(scope, elm, attrs) {
            //Way One
            scope.hello = "some message";
            scope.fromDirectiveFn(scope.hello);
        }
    }
});

Just replacing the & to = . 只需将&替换为= Working fork: http://jsfiddle.net/L8masomq/ 工作叉: http : //jsfiddle.net/L8masomq/

its bit of a quirk... read more about it here 这有点古怪... 在这里了解更多

what you need to do is - change below 您需要做的是-在下面进行更改

scope.fromDirectiveFn(scope.hello);

to

 scope.fromDirectiveFn({'msg' : scope.hello});

the variable names should be same in caller & callee 变量名称在调用方和被调用方中应相同

Actually, the code is correct. 实际上,该代码是正确的。 But, the value of msg which is getting undefined. 但是,msg的值变得不确定。 Thus, you ctrlFn(msg) is returning as undefined. 因此,您的ctrlFn(msg)返回为未定义。 adding the 'msg' property and assigning the scope.hello will resolve the problem. 添加'msg'属性并分配scope.hello将解决此问题。 Check the improved [Jsfiddle][1] here. 在此处检查改进的[Jsfiddle] [1]。

scope.fromDirectiveFn({'msg': scope.hello}); 

http://jsfiddle.net/imsabmaverick81/j2K7N/215/ http://jsfiddle.net/imsabmaverick81/j2K7N/215/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM