第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何創(chuàng)建單獨(dú)的AngularJS控制器文件?

如何創(chuàng)建單獨(dú)的AngularJS控制器文件?

PIPIONE 2019-08-06 13:18:34
如何創(chuàng)建單獨(dú)的AngularJS控制器文件?我將所有AngularJS控制器都放在一個(gè)文件controllers.js中。該文件的結(jié)構(gòu)如下:angular.module('myApp.controllers', [])   .controller('Ctrl1', ['$scope', '$http', function($scope, $http) {       }])   .controller('Ctrl2', ['$scope', '$http', function($scope, $http) }   }])我想做的是將Ctrl1和Ctrl2放入單獨(dú)的文件中。然后我會(huì)在index.html中包含這兩個(gè)文件,但是應(yīng)該如何構(gòu)建呢?我嘗試做這樣的事情,它在Web瀏覽器控制臺(tái)中拋出一個(gè)錯(cuò)誤,說(shuō)它無(wú)法找到我的控制器。任何提示?
查看完整描述

3 回答

?
侃侃無(wú)極

TA貢獻(xiàn)2051條經(jīng)驗(yàn) 獲得超10個(gè)贊

文件一:

angular.module('myApp.controllers', []);

文件二:

angular.module('myApp.controllers').controller('Ctrl1', ['$scope', '$http', function($scope, $http){}]);

檔案三:

angular.module('myApp.controllers').controller('Ctrl2', ['$scope', '$http', function($scope, $http){}]);

包括在那個(gè)順序中。我推薦3個(gè)文件,因此模塊聲明是獨(dú)立的。


關(guān)于文件夾結(jié)構(gòu),關(guān)于這個(gè)主題有很多很多意見(jiàn),但這兩個(gè)都很不錯(cuò)

https://github.com/angular/angular-seed

http://briantford.com/blog/huuuuuge-angular-apps.html


查看完整回答
反對(duì) 回復(fù) 2019-08-06
?
DIEA

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超2個(gè)贊

在末尾使用帶有數(shù)組的angular.module API 將告訴angular創(chuàng)建一個(gè)新模塊:

myApp.js

// It is like saying "create a new module"angular.module('myApp.controllers', []); // Notice the empty array at the end here

在沒(méi)有數(shù)組的情況下使用它實(shí)際上是一個(gè)getter函數(shù)。因此,要分離您的控制器,您可以:

Ctrl1.js

// It is just like saying "get this module and create a controller"angular.module('myApp.controllers').controller('Ctrlr1', ['$scope', '$http', function($scope, $http) {}]);

Ctrl2.js

angular.module('myApp.controllers').controller('Ctrlr2', ['$scope', '$http', function($scope, $http) {}]);

在你的javascript導(dǎo)入過(guò)程中,只需確保myApp.js在AngularJS之后,但在任何控制器/服務(wù)/等之前...否則angular將無(wú)法初始化你的控制器。


查看完整回答
反對(duì) 回復(fù) 2019-08-06
?
犯罪嫌疑人X

TA貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個(gè)贊

雖然這兩個(gè)答案在技術(shù)上都是正確的,但我想為此答案引入不同的語(yǔ)法選擇。這個(gè)imho可以更容易地閱讀注射的內(nèi)容,區(qū)分等。


文件一


// Create the module that deals with controllers

angular.module('myApp.controllers', []);

文件二


// Here we get the module we created in file one

angular.module('myApp.controllers')


// We are adding a function called Ctrl1

// to the module we got in the line above

.controller('Ctrl1', Ctrl1);


// Inject my dependencies

Ctrl1.$inject = ['$scope', '$http'];


// Now create our controller function with all necessary logic

function Ctrl1($scope, $http) {

  // Logic here

}

文件三


// Here we get the module we created in file one

angular.module('myApp.controllers')


// We are adding a function called Ctrl2

// to the module we got in the line above

.controller('Ctrl2', Ctrl2);


// Inject my dependencies

Ctrl2.$inject = ['$scope', '$http'];


// Now create our controller function with all necessary logic

function Ctrl2($scope, $http) {

  // Logic here

}


查看完整回答
反對(duì) 回復(fù) 2019-08-06
  • 3 回答
  • 0 關(guān)注
  • 801 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)