誰かが私にこれら2つのコードとその管理方法の違いを教えてください。anglejsを使用しているマークアップでスコープ変数を使用する
:ここは、ここに私のindex.htmlを
<body ng-app="docsIsolateScopeDirective">
<div ng-controller="Controller">
<my-customer info="naomi" ></my-customer>
{{nameUser}}
<my-customer info="igor"></my-customer>
</div>
</body>
は私script.js
(function(angular) {
'use strict';
angular.module('docsIsolateScopeDirective', [])
.controller('Controller', ['$scope', function($scope) {
$scope.nameUser = 'naomi';
$scope.naomi = { name: 'Naomi', address: '1600 Amphitheatre' };
$scope.igor = { name: 'Igor', address: '123 Somewhere' };
}])
.directive('myCustomer', function() {
return {
restrict: 'E',
scope: {
customerInfo: '=info'
},
templateUrl: 'my-customer-iso.html'
};
});
})(window.angular);
そして最後に、私の私の顧客-iso.htmlです
Name: {{customerInfo.name}} Address: {{customerInfo.address}}
結果がこれです:
Name: Naomi Address: 1600 Amphitheatre
naomi Name: Igor Address: 123 Somewhere
質問です:私は(「ナオミ」が含まれている基本的には$ scope.nameUser)スコープ変数と私のマークアップの情報を設定したいと思います。しかし、私は
<my-customer info={{nameUser}} </my-customer> // or <my-customer info="{{nameUser}}"> </my-customer>
代わりの
<my-customer info= "naomi"></my-customer>
コードを記述する際に壊れていると..スコープ変数である理由私は理解していない文字列のように考えることができないのですか?おかげ
編集:私が書く場合でも:
<my-customer info = nameUser> </my-customer>
結果はまだ異なっています。エラーは発生しませんが、出力は次のようになります。
Name: Address:
Name: Igor Address: 123 Somewhere
コントローラとディレクティブとのやり取りについては何か不足していると思います。参照:https://plnkr.co/edit/hbi4whoH3Dj9lDV3vVzZ?p=preview
ありがとうございますが、うまくいきません。エラーは表示されませんが、結果は「名前:住所:」 – Jibeee
私の回答が更新されました。これを明示してください。 –
私は間違いを今理解しています。私は "naomi"は$ scope.naomiを参照する文字列であり、オブジェクト自体ではないと考えました。あなたの説明をありがとう – Jibeee