การฉีดยาอนุมาน
วิธีการฉีดนี้ต้องการการตรวจสอบให้แน่ใจว่าชื่อพารามิเตอร์เหมือนกับชื่อบริการ หากรหัสจำเป็นต้องถูกบีบอัด ฯลฯ การฉีดจะล้มเหลว
app.controller ("MyCtrl1", ฟังก์ชั่น ($ scope, hello1, hello2) {$ scope.hello = function () {hello1.hello (); hello2.hello ();}});การฉีดเครื่องหมาย
วิธีการฉีดนี้ต้องการการตั้งค่าอาร์เรย์การพึ่งพาซึ่งมีชื่อบริการพึ่งพา ในพารามิเตอร์ฟังก์ชั่นคุณสามารถตั้งค่าชื่อพารามิเตอร์ได้ตามต้องการ แต่ต้องมั่นใจในความสอดคล้องของคำสั่งซื้อ
var myctrl2 = function ($ scope, hello1, hello2) {$ scope.hello = function () {hello1.hello (); Hello2.hello (); }} myctrl2. $ injector = ['hello1', 'hello2']; App.Controller ("MyCtrl2", MyCtrl2);การฉีดแบบอินไลน์
วิธีการฉีดนี้ผ่านสองพารามิเตอร์หนึ่งคือชื่อและอีกวิธีหนึ่งคืออาร์เรย์ พารามิเตอร์สุดท้ายของอาร์เรย์นี้เป็นวิธีการที่แท้จริงและส่วนที่เหลือคือการพึ่งพาทั้งหมด แต่มีความจำเป็นเพื่อให้แน่ใจว่าพารามิเตอร์ในร่างกายของวิธีการอยู่ในลำดับเดียวกัน (เหมือนกับการฉีดมาร์ค)
app.controller ("MyCtrl3", ['$ scope', 'hello1', 'hello2', ฟังก์ชั่น ($ scope, hello1, hello2) {$ scope.hello = function () {hello1.hello (); hello2.hello ();}}]);วิธีการทั่วไปของ $ หัวฉีด
ในเชิงมุมหัวฉีดสามารถรับได้ผ่าน angular.injector ()
var $ injector = angular.injector ();
รับชื่อบริการพึ่งพาผ่าน $ injector.get ('serviceName')
$ injector.get ('$ scope')
รับการพึ่งพาทั้งหมดของ XXX โดย $ injector.annotate ('xxx')
$ injector.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.min.min.min.min.min.min.min.min.min.min.min.min.min.min.min.min.min.min.min.js ng-controller = "myctrl1"> <อินพุต type = "ปุ่ม" ng-click = "hello ()" value = "ctrl1"> </อินพุต> </div> <div ng-controller = "myctrl2"> <อินพุต type = " ng-controller = "myctrl3"> <อินพุต type = "ปุ่ม" ng-click = "hello ()" value = "ctrl3"> </input> </div> <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 ("Hello Service");}}}); var $ injector = angular.injector (); console.log (Angular.equals ($ injector.get ('$ injector'), $ injector)); // true console.log (angular.equals ($ injector.invoke (ฟังก์ชั่น ($ injector) {return $ injector;}), $ injector); app.controller ("MyCtrl1", ฟังก์ชั่น ($ scope, hello1, hello2) {$ scope.hello = function () {hello1.hello (); hello2.hello ();}}); // คำอธิบายประกอบ // ฟังก์ชั่นอย่างชัดเจน (serviceA) {}; // ชัดเจน. $ inject = ['ServiceA']; // $ injector.invoke (ชัดเจน); var myctrl2 = function ($ scope, hello1, hello2) {$ scope.hello = function () {hello1.hello (); Hello2.hello (); }} myctrl2. $ injector = ['hello1', 'hello2']; App.Controller ("MyCtrl2", MyCtrl2); // inline app.controller ("myctrl3", ['$ scope', 'hello1', 'hello2', ฟังก์ชั่น ($ scope, hello1, hello2) {// app.controller ("myctrl3", ['$ scope', 'hello1', 'hello2' c.hello ();ข้างต้นเป็นการรวบรวมข้อมูลของหัวฉีด AngularJs เราจะยังคงเพิ่มข้อมูลที่เกี่ยวข้องในอนาคต ขอบคุณสำหรับการสนับสนุนเว็บไซต์นี้!