繁体   English   中英

AngularJs-使用父级范围变量/隔离范围的指令

[英]AngularJs - Directive Using Parent Scope Vars W/ Isolate Scope

我什至不知道此时要问什么(编辑:我已经得知,我不清楚我的问题是“为什么指令没有看到传递给它的属性”),我以为我理解了。 似乎不能在指令内使用父控制器的属性(是的,它们确实需要是可选的,但我不认为这是问题所在)。

我以为我创建了一个隔离范围: scope: {myVar: "=?"}然后简单地创建一个myVar属性并将其指向父控制器上的“源”。 我究竟做错了什么?

小提琴: http : //jsfiddle.net/x0mukxdd/

HTML:

<my-directive isDisabledDirectiveVar = "isDisabledOuterVar" insideDirectiveTestString = "someTestString" />

js:

var app = angular.module("myApp", []);
app.controller("myController", function ($scope) {
    $scope.isDisabledOuterVar = true;
    $scope.someTestString = 'blahblah I am a astring';
});

app.directive("myDirective", function () {
    return {
        restrict: 'E',
        scope: {
            isDisabledDirectiveVar: '=?',
            insideDirectiveTestString: '=?'
        },
        template: '<input type = "text" ng-disabled= "isDisabledDirectiveVar"' +
            'value = "{{insideTestString}}" ></input>'
    };
});

旁注,参考文章在这里。

Angular在javascript中使用camelCase,但是将其从HTML中的破折号转换为。

html: <my-directive is-disabled-directive-var="yourVar" />

javascript: scope: { isDisabledDirectiveVar: '=?' } scope: { isDisabledDirectiveVar: '=?' }

更新的示例: http : //jsfiddle.net/x0mukxdd/2/

暂无
暂无

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

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