حقن مستنتج
تتطلب طريقة الحقن ضمان أن اسم المعلمة هو نفس اسم الخدمة. إذا كان الكود يحتاج إلى ضغط ، وما إلى ذلك ، فسوف تفشل الحقن.
app.controller ("myctrl1" ، function ($ scope ، hello1 ، hello2) {$ scope.hello = function () {hello1.hello () ؛ hello2.hello () ؛}}) ؛حقن علامة
تتطلب طريقة الحقن هذه تعيين صفيف التبعية ، والذي يحتوي على اسم خدمة التبعية. في معلمات الوظيفة ، يمكنك تعيين اسم المعلمة في الإرادة ، ولكن يجب ضمان اتساق الطلب.
var myctrl2 = function ($ scope ، hello1 ، hello2) {$ scope.hello = function () {hello1.hello () ؛ hello2.hello () ؛ }} myctrl2. $ enjector = ['hello1' ، 'hello2'] ؛ App.Controller ("myctrl2" ، myctrl2) ؛الحقن المضمّن
تمر طريقة الحقن هذه مباشرة في معلمتين ، أحدهما هو الاسم والآخر عبارة عن صفيف. المعلمة الأخيرة من هذه الصفيف هي هيئة الطريقة الحقيقية ، والباقي كلها تبعيات ، ولكن من الضروري التأكد من أن المعلمات في الجسم في نفس الترتيب (نفس حقن العلامة).
App.Controller ("myctrl3" ، ['$ scope' ، 'hello1' ، 'hello2' ، function ($ scope ، hello1 ، hello2) {$ scope.hello = function () {hello1.hello () ؛ hello2.hello () ؛}}]) ؛الطرق الشائعة للحاقن $
في الزاوي ، يمكن الحصول على الحاقن من خلال angular.injector ().
var $ enjector = Angular.Injector () ؛
احصل على اسم خدمة التبعية من خلال $ enjector.get ('servicename')
$ enjector.get ('$ scope')
احصل على جميع التبعيات من xxx بواسطة $ enjector.annotate ('xxx')
$ enjector.annotate (xxx)
نموذج الرمز
<html> <head> <meta http-equiv = "content-type" content = "text/html ؛ charset = utf-8"/> <script src = "http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min <div ng-controller = "myctrl1"> <input type = "button" ng-click = "hello ()" value = "ctrl1"> </pection> </viv> <div ng-controller = "myctrl2"> <input type = "button" ng-controller = "myctrl3"> <input type = "button" ng-click = "hello ()" value = "ctrl3"> </pection> </viv> <script type = "text/javaScript"> var app = Angular.Module ("myapp" ، []) ؛ app.factory ("hello1" ، function () {return {hello: function () {console.log ("hello1 service") ؛}}}) ؛ app.factory ("hello2" ، function () {return {hello: function () {console.log ("hello2 service") ؛}}}) ؛ var $ enjector = Angular.Injector () ؛ console.log (Angular.equals ($ enjector.get ('$ enjector') ، $ enjector)) ؛ // true console.log (Angular.equals ($ enjuctor.invoke (function ($ enjector) {return $ enjuctor ؛}) ، $ justor)) ؛ app.controller ("myctrl1" ، function ($ scope ، hello1 ، hello2) {$ scope.hello = function () {hello1.hello () ؛ hello2.hello () ؛}}) ؛ // annotated // وظيفة صريحة (servicea) {} ؛ // صريح. $ enject = ['servicea'] ؛ // $ enjector.invoke (صريح) ؛ var myctrl2 = function ($ scope ، hello1 ، hello2) {$ scope.hello = function () {hello1.hello () ؛ hello2.hello () ؛ }} myctrl2. $ enjector = ['hello1' ، 'hello2'] ؛ App.Controller ("myctrl2" ، myctrl2) ؛ // inline app.controller ("myctrl3" ، ['$ scope' ، 'hello1' ، 'hello2' ، function ($ scope ، hello1 ، hello2) {// app.controller ("myctrl3" ، ['$ scope' ، 'hello1' ، 'hello2' ، function (a ، c) {// C.Hello () ؛ما سبق هو تجميع معلومات عن حاقن AngularJS. سنستمر في إضافة المعلومات ذات الصلة في المستقبل. شكرا لدعمكم لهذا الموقع!