简体   繁体   English

自定义指令对角度属性的困惑

[英]Confusion on Angular Attributes on Custom Directive

I'm confused as to why this isn't working... 我很困惑为什么这不起作用...

here is my directive: 这是我的指令:

app.directive('dateDropDowns', function () {

function link($scope, elem, attr) {

    console.log($scope.startYear);
};

function controller($scope, $attrs, $location) {

};

return {
    link: link,
    scope: {
        startYear: "&",
        endYear: "&",
        date: "=",
        required: "&"
    },
    restrict: "EA",
    templateUrl: "/scripts/templates/datedropdowns.html",
    controller: controller
}
});

Here is my directive html: 这是我的指令html:

 <div date-drop-downs start-year="startYear" end-year="endYear" required="true" date="testDate"></div>

startYear , endYear , testDate are all defined in the scope of the controller on which the above html exists. startYearendYeartestDate都在存在上述html的控制器范围内定义。

The console.log in the link function is returning: 链接功能中的console.log返回:

 function (locals) {
              return parentGet(scope, locals);
            } 

However, the startYear should be returning 1910 as that is what is set to in the controller on which the "date-drop-downs" directive is being called. 但是,startYear应该返回1910,因为这是在调用“ date-drop-downs”指令的控制器中设置的值。

I've tried console.log($scope.$eval($scope.startYear)); 我试过console.log($scope.$eval($scope.startYear)); but it returns the same thing above 但它返回上面的相同的东西

I've tried console.log($scope.$eval(attr.startYear)); 我已经尝试过console.log($scope.$eval(attr.startYear)); but it returns the same thing above 但它返回上面的相同的东西

I've tried console.log(attr.startYear); 我试过console.log(attr.startYear); but that just returns the text "startYear" 但这只是返回文本“ startYear”

I'm not sure what to do to actually get the startYear value. 我不确定该如何做才能真正获得startYear值。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

Since you are using & binding they get wrapped in as function if they are already not function. 由于您正在使用&绑定,如果它们已经不起作用,它们将被包装为函数。 This is generally used to invoke a method on the parent using isolate scope. 通常使用隔离范围在父对象上调用方法。

So you should actually do:- 因此,您实际上应该这样做:

$scope.startYear()

to get the value of startYear, if you are looking for 2 waybinding you should go with = only, if you just need it as text binding then use @ and set the value as start-year="{{startYear}}" on the directive 要获取startYear的值,如果您正在寻找2种方式绑定,则应仅使用= ,如果只需要将其用作文本绑定,则使用@并将值设置为start-year="{{startYear}}"指示

& or &attr - provides a way to execute an expression in the context of the parent scope. &或&attr-提供一种在父作用域的上下文中执行表达式的方法。 If no attr name is specified then the attribute name is assumed to be the same as the local name. 如果未指定attr名称,则假定属性名称与本地名称相同。 Given and widget definition of scope: { localFn:'&myAttr' }, then isolate scope property localFn will point to a function wrapper for the count = count + value expression. 给定和小部件的作用域定义:{localFn:'&myAttr'},然后隔离作用域属性localFn将指向count = count + value表达式的函数包装。 Often it's desirable to pass data from the isolated scope via an expression to the parent scope, this can be done by passing a map of local variable names and values into the expression wrapper fn. 通常希望将数据从隔离范围通过表达式传递到父范围,这可以通过将局部变量名称和值的映射传递到表达式包装器fn中来完成。 For example, if the expression is increment(amount) then we can specify the amount value by calling the localFn as localFn({amount: 22}). 例如,如果表达式为增量(金额),则可以通过将localFn调用为localFn({amount:22})来指定金额值。

Inorder to achieve one-time binding you could use bindonce or with angular 1.3beta you could use one-time binding syntax :- 为了实现一次性绑定,您可以使用bindonce或与angular 1.3beta一起使用,您可以使用一次性绑定语法 :-

 .. start-year="{{::startYear}}" ...

and

 startYear: "@",

Plnkr Plnkr

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

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