0
我開始學習AngularJS我被困在創建我自己的服裝指令。AngularJS指令作用域似乎只接受一個參數
所以,首先,我使用yeoman項目來生成一個AngularJS項目,這是我的出發點。
現在,以主題 -
我有以下代碼:
app.js:
myapp.directive('userinfo', function() {
var directive = {};
directive.restrict = 'E';
directive.template = '<b>User: {{ user.firstName }} {{ user.lastName}}</b>';
directive.scope = {
user : '='/*, editing: false*/
};
return directive;
});
的index.html:
<body ng-app="proj2App" ng-controller="MainCtrl">
...
<userinfo user="users[0]"> </userinfo>
...
</body>
個main.js:
angular.module('proj2App')
.controller('MainCtrl', function ($scope) {
$scope.users = [
{ firstName : 'John', lastName: 'Doe'}
];
});
我的問題,因爲它似乎是在該行app.js在那裏我定義directive.scope發現。每當它是這樣的:
directive.scope = {
user : '='/*, editing: false*/
};
是沒有問題的,一切工作就好了..但是,當我刪除註釋塊,它看起來像:
directive.scope = {
user : '=', editing: false
};
它不工作 - 網頁不會顯示模板和角度會拋出下面的錯誤,我絕對沒說什麼:
**TypeError**: undefined is not a function
at http://localhost:9000/bower_components/angular/angular.js:6436:30
at forEach (http://localhost:9000/bower_components/angular/angular.js:331:20)
at parseIsolateBindings (http://localhost:9000/bower_components/angular/angular.js:6435:5)
at http://localhost:9000/bower_components/angular/angular.js:6494:49
at forEach (http://localhost:9000/bower_components/angular/angular.js:323:20)
at Object.<anonymous> (http://localhost:9000/bower_components/angular/angular.js:6480:13)
at Object.invoke (http://localhost:9000/bower_components/angular/angular.js:4182:17)
at Object.enforcedReturnValue [as $get] (http://localhost:9000/bower_components/angular/angular.js:4035:37)
at Object.invoke (http://localhost:9000/bower_components/angular/angular.js:4182:17)
at http://localhost:9000/bower_components/angular/angular.js:4000:37
是否有人知道這裏發生了什麼?
謝謝,真是棒極了!任何理由爲什麼它不以另一種方式工作? – user3848844
獨立作用域用於將數據綁定到指令中的作用域。例如。綁定來自Controller的數據。但是如果不需要數據綁定並且需要在指令範圍中使用變量,那麼您只需在鏈接函數中的範圍內聲明即可。希望有幫助 – dcodesmith
確實有幫助。謝謝 :) – user3848844