![](/img/trans.png)
[英]Passing a string description of an Angular scope variable to a directive
[英]Passing a variable with an angular directive
我想通過模板將變量傳遞給指令。 我甚至在我自己的代碼中都有工作示例,而且在我的生活中,我不能弄清楚為什么這個示例不起作用。
我確定這只是一個缺失的角色 - 某個地方的'@'或'$'。
var app = angular.module('myApp', ['ngGrid']); app.controller('MyCtrl', function($scope) { $scope.foo = 'baz'; }). directive('myFnord', function() { return{ scope: { foo: '@' }, link: function(scope, element, attrs) { console.log('!'); console.log(scope.foo); } } } );
.gridStyle { border: 1px solid rgb(212,212,212); width: 600px; height: 300px; }
<!DOCTYPE html> <html ng-app="myApp"> <head lang="en"> <meta charset="utf-8"> <title>Custom Plunker</title> <link rel="stylesheet" type="text/css" href="http://angular-ui.github.com/ng-grid/css/ng-grid.css" /> <link rel="stylesheet" type="text/css" href="style.css" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js"></script> <script type="text/javascript" src="http://angular-ui.github.com/ng-grid/lib/ng-grid.debug.js"></script> <script type="text/javascript" src="main.js"></script> </head> <body ng-controller="MyCtrl"> <div foo='foo' my-fnord>:)</div> </body> </html>
這里是largeLoad.json
:
[{"name": "Moroni", "allowance": 505050505050505050, "paid": true},
{"name": "Tiancum", "allowance": 53, "paid": false},
{"name": "Jacob", "allowance": 27, "paid": false},
{"name": "Nephi", "allowance": 29, "paid": false},
{"name": "Enos", "allowance": 34, "paid": false},
{"name": "Ether", "allowance": 42, "paid": false},
{"name": "Alma", "allowance": 43, "paid": true},
{"name": "Jared", "allowance": 21, "paid": true},
{"name": "Moroni", "allowance": 50, "paid": true},
{"name": "Tiancum", "allowance": 53, "paid": false},
{"name": "Jacob", "allowance": 27, "paid": false},
{"name": "Nephi", "allowance": 29, "paid": false},
{"name": "Enos", "allowance": 34, "paid": false},
{"name": "Ether", "allowance": 42, "paid": false},
{"name": "Alma", "allowance": 43, "paid": true},
{"name": "Jared", "allowance": 21, "paid": true},
{"name": "Moroni", "allowance": 50, "paid": true},
{"name": "Tiancum", "allowance": 53, "paid": false},
{"name": "Jacob", "allowance": 27, "paid": false},
{"name": "Nephi", "allowance": 29, "paid": false},
{"name": "Enos", "allowance": 34, "paid": false},
{"name": "Ether", "allowance": 42, "paid": false},
{"name": "Alma", "allowance": 43, "paid": true},
{"name": "Jared", "allowance": 21, "paid": true},
{"name": "Moroni", "allowance": 50, "paid": true},
{"name": "Tiancum", "allowance": 53, "paid": false},
{"name": "Jacob", "allowance": 27, "paid": false},
{"name": "Nephi", "allowance": 29, "paid": false},
{"name": "Enos", "allowance": 34, "paid": false},
{"name": "Ether", "allowance": 42, "paid": false},
{"name": "Alma", "allowance": 43, "paid": true},
{"name": "Jared", "allowance": 21, "paid": true},
{"name": "Moroni", "allowance": 50, "paid": true},
{"name": "Tiancum", "allowance": 53, "paid": false}]
在你的指令中,改變
scope: {
foo: '@'
},
至
scope: {
foo: '='
},
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.