AngularJS – 为什么我的指令隔离范围变量未定义?
发布时间:2020-12-17 09:41:58 所属栏目:安全 来源:网络整理
导读:我有以下的朋友: http://plnkr.co/edit/7YUpQ1tEjnUaX01txFcK?p=preview 当我运行它时,templateUrl在范围中未定义.为什么? 我的假设是,它试图在父范围中找到名为template.html的变量,但不能,因此将其分配给undefined.如果是这样,我如何将它作为字符串而不
我有以下的朋友:
http://plnkr.co/edit/7YUpQ1tEjnUaX01txFcK?p=preview 当我运行它时,templateUrl在范围中未定义.为什么? 我的假设是,它试图在父范围中找到名为template.html的变量,但不能,因此将其分配给undefined.如果是这样,我如何将它作为字符串而不是范围变量传递? HTML: <body ng-app="myApp"> <div ng-controller="TestCtrl"> <test-directive ng-model="testModel" template-url="template.html"> </test-directive> </div> </body> .js文件 var app = angular.module("myApp",[]); app.controller("TestCtrl",function($scope) { $scope.testModel = {} }); app.directive("testDirective",function () { return { restrict: 'E',scope: { model: "=ngModel",templateUrl: "=" },template: "<div ng-include='templateUrl'></div>",link: function (scope,element,attrs) { console.log(scope.templateUrl); // <-- Shows as undefined } } });
只是改变范围:
scope: { templateUrl: "@" }, 你会得到输出’template.html’. 关键是’=’和’@’之间的区别.你可以参考https://docs.angularjs.org/guide/directive. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |